I have an npm project with the following folder structure:
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. And my server.js is the back end. I'm using nodemon (version 2.0.16) to run my server.js file, which is a simple Express app:
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. However, the Nodemon docs say:
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).
So, I figure that if my package.json file looks like this
{
"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. 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. Can anyone explain why I can't just run nodemon without arguments in this case?
It appears that if I delete index.js, running the no-argument nodemon command works as expected. 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.
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.