node.js - Nodemon 一直在寻找 index.js

标签 node.js express nodemon

我的 Node 监视器 一直在寻找index.js ,但我想使用 app.js大多数人现在使用app.js .

enter image description here

如何更新我的 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...

包.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 这样的云应用程序平台需要在你的 package.json 中添加“npm start”才能执行你的应用程序。如果您需要从命令行加载其他 npm 模块,将 npm 命令添加到项目中也很有用。

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

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

关于node.js - Nodemon 一直在寻找 index.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49820328/

相关文章:

javascript - Express.js + Mongoose。 find() 未传递定义变量的结果

javascript - 部署Node App后如何在Heroku上安装依赖项?

javascript - VS Code 像 nodemon 一样在文件保存时自动重启调试器

node.js - Mongoose 找到所有不发送回调

node.js - 如何在连接中向中间件添加模式?

javascript - 无法在生产模式下升起 sails 。(sails lift --prod --verbose 不起作用)

node.js - 在 docker : Error opening Api. yaml 中使用 pm2 运行 Node.js/Swagger 应用程序

javascript - 当进程崩溃时,Nodemon 丢失控制台输出

json - 更新 mongodb 集合内的 json 对象 : mongoose

javascript - http服务器端node.js数据