Nodemon 一直在寻找 index.js

kyo*_*kyo 4 node.js express nodemon

我的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...
Run Code Online (Sandbox Code Playgroud)

包.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"
  }
}
Run Code Online (Sandbox Code Playgroud)

Hen*_*ges 10

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"
}
Run Code Online (Sandbox Code Playgroud)

你可以把"main": index.js"改成"main":"app.js"。或者你可以运行"nodemon filename"来指定入口文件。我通常做的是在我的package.json中添加脚本。例如:

"scripts": {
    "start": "node app.js",
    "test": "nodemon app.js"
},
Run Code Online (Sandbox Code Playgroud)

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

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

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