简体   繁体   English

npm 项目中的无参数 nodemon 命令不会从 package.json 运行“main”的值

[英]Argumentless nodemon command in npm project does not run value of "main" from package.json

I have an npm project with the following folder structure:我有一个具有以下文件夹结构的 npm 项目:

project
├── node_modules
│   └── (node_modles folders)
├── server.js
├── index.js
├── index.html
├── package.json
└── package-lock.json

My index.js is my front end JavaScript code of this little Web app.我的index.js是这个小 Web 应用程序的前端 JavaScript 代码。 And my server.js is the back end.我的server.js是后端。 I'm using nodemon (version 2.0.16) to run my server.js file, which is a simple Express app:我正在使用 nodemon(版本 2.0.16)来运行我的server.js文件,这是一个简单的 Express 应用程序:

const express = require('express');
const app = express();
app.listen(4000, () => {console.log('Listening on port 4000')})

To run my server, I run nodemon server.js in my terminal, and my Express app runs correctly.为了运行我的服务器,我在终端中运行nodemon server.js ,我的 Express 应用程序运行正常。 However, the Nodemon docs say:但是,Nodemon 文档说:

If you have a package.json file for your app, you can omit the main script entirely and nodemon will read the package.json for the main property and use that value as the app ( ref ).如果您的应用程序有 package.json 文件,则可以完全省略主脚本,nodemon 将读取 package.json 的 main 属性并将该值用作应用程序( ref )。

So, I figure that if my package.json file looks like this所以,我想如果我的package.json文件看起来像这样

{
  "main": "server.js",
  "dependencies": {
    "express": "^4.18.1"
  }
}

then I should be able to just run nodemon instead of nodemon server.js , and everything should work the same.那么我应该能够只运行nodemon而不是nodemon server.js ,并且一切都应该正常工作。 However, this is not the case.然而,这种情况并非如此。 When I simply run nodemon with this setup, it's apparently trying to run my index.js file regardless of what I have for "main" in my package.json file.当我使用此设置简单地运行nodemon时,它显然是在尝试运行我的index.js文件,而不管我的package.json文件中的"main"是什么。 Can anyone explain why I can't just run nodemon without arguments in this case?谁能解释为什么在这种情况下我不能只运行没有参数的 nodemon ?

It appears that if I delete index.js , running the no-argument nodemon command works as expected.看来,如果我删除index.js ,运行 no-argument nodemon命令会按预期工作。 However, if there is a file named index.js at the root of the project, then that file will get run no matter what I put for "main" in package.json .但是,如果在项目的根目录中有一个名为index.js的文件,那么无论我在package.json中为"main"添加什么内容,该文件都会运行。

I'm not sure if this is working-as-designed or if it was intended to have the "main" value ignored like this.我不确定这是否按设计工作,或者是否打算像这样忽略“主要”值。 I couldn't find a mention of the precedence in the docs.我在文档中找不到优先级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM