【问题标题】:How can I automatic start and reload index.js in nodeJs, when I do changes on my frontend - Angular?当我在前端进行更改时,如何在 nodeJs 中自动启动和重新加载 index.js - Angular?
【发布时间】:2022-01-13 03:17:10
【问题描述】:

我知道,我必须更改我的 package.json 文件,但我不明白怎么做。 我尝试在 package.json 脚本中使用“watch”,但它不起作用。 请帮我。我刚开始学习 Angular 和 NodeJs。 对不起我的英语不好和简单的问题。 现在我总是用 nodemon index.js 重新加载我的服务器并且有问题: enter image description here

  "dependencies": {
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "mysql2": "^2.3.3",
    "node": "^17.1.0"
  },
  "name": "server",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "description": "",
  "devDependencies": {
    "nodemon": "^2.0.15"
  }
}

【问题讨论】:

    标签: node.js angular


    【解决方案1】:

    我相信你在src 文件夹中有index.js

    使用环境设置

      .
      . 
      "scripts": {
        "start": "node src/index.js",
        "dev": "***env-cmd -f ./config/dev.env*** nodemon src/index.js",
        "test": "env-cmd -f ./config/test.env jest --watch"
      },
     .
     .
    

    无环境并且想要跟踪js以外的文件扩展名的变化。

    如果您想在 js, hbs, html 文件更改时自动启动服务器。

      .
      . 
      "scripts": {
        "start": "node src/index.js",
        "dev": "env-cmd -f ./config/dev.env nodemon src/index.js -e js,hbs,html",
        "test": "env-cmd -f ./config/test.env jest --watch"
      },
     .
     .
    

    没有环境并且只跟踪js文件

    以下部分显示了没有环境设置的命令。 这里我们完全忽略了-e 参数,因为我们不希望在.js 以外的文件发生更改时自动启动服务器。

      .
      . 
      "scripts": {
        "start": "node src/index.js",
        "dev": "nodemon src/index.js",
        "test": "env-cmd -f ./config/test.env jest --watch"
      },
     .
     .
    

    在命令提示符下。运行以下命令:

    $npm run dev
    

    注意:-您可以忽略上述解决方案中的starttest

    【讨论】:

    • 这不起作用。同样的错误,出现在屏幕截图`“scripts”:{“start”:“node index.js”,“dev”:“nodemon index.js”,“test”:“env-cmd -f ./config/ test.env jest --watch" } ` 第一个和第二个不起作用。 env-cmd 的问题。 “*** env-cmd”不是内部或外部命令、可执行程序或批处理文件
    • 第 1 和第 2 仅在您设置了 dev/prod/staging 环境时才有效。参考:npmjs.com/package/env-cmd。参考设置 medium.com/the-node-js-collection/... – Abhishek 37 分钟
    猜你喜欢
    • 1970-01-01
    • 2016-01-21
    • 2020-11-08
    • 2019-11-11
    • 2016-04-26
    • 2021-03-03
    • 1970-01-01
    • 2020-03-28
    相关资源
    最近更新 更多