Nodemon 一直在寻找 index.js

Nodemon kept looking for index.js

我的 Nodemon 一直在寻找 index.js,但我想使用 app.js,因为现在大多数人都使用 app.js

如何更新我的 nodemon 以查找 app.js?

我试过卸载,重装也无济于事。


⚡️  vidjot  nodemon
[nodemon] 1.17.3
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node index.js`
module.js:540
    throw err;
    ^

Error: Cannot find module '/Users/bheng/Sites/vidjot/index.js'
    at Function.Module._resolveFilename (module.js:538:15)
    at Function.Module._load (module.js:468:25)
    at Function.Module.runMain (module.js:684:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3
[nodemon] app crashed - waiting for file changes before starting...

package.json

{
  "name": "vidjot",
  "version": "1.0.0",
  "description": "app to create video idea",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.16.3"
  }
}

Nodemon 命令在您的 package.json 文件中搜索主 属性 并尝试执行它的文件。示例:

{
  "name": "app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "henriquelborges",
  "license": "ISC"
}

可以将"main":index.js"改为"main":"app.js"。或者可以运行"nodemon filename"指定条目文件。我通常做的是将脚本添加到我的 package.json。示例:

"scripts": {
    "start": "node app.js",
    "test": "nodemon app.js"
},

在此之后,我只需在我的项目根文件夹中使用 "npm start" 或 "npm test" 之类的命令。像 Heroku 这样的云应用程序平台需要 "npm start" 在您的 package.json 才能执行您的应用程序。如果需要从命令行加载其他 npm 模块,将 npm 命令添加到项目中也很有用。

示例 - 您可以加载环境变量以在本地主机上测试您的应用程序:

"test": "nodemon -r dotenv/config app.js dotenv_config_path=./config.env"

它应该是 app.js 而不是 index.js 因为:

...
"main": "app.js",
...

nodemon documentation 所述,您只需在参数中指定应用程序的入口点:

nodemon ./app.js

或者您可以在 package.json 文件中指定它:

{
  "name": "app",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "henriquelborges",
  "license": "ISC"
}

您应该编辑 package.json 中的主要值:

变化:

{
...
"main":"index.js"
...
}

至:

{
...
 "main":"app.js"
...
}