How to execute the start script with Nodemon
node.jsNodemonnode.js Problem Overview
How can I execute the start script from a package.json file with Nodemon?
node.js Solutions
Solution 1 - node.js
This will be a simple command for this
nodemon --exec npm start
Solution 2 - node.js
In package json:
{
"name": "abc",
"version": "0.0.1",
"description": "my server",
"scripts": {
"start": "nodemon my_file.js"
},
"devDependencies": {
"nodemon": "~1.3.8",
},
"dependencies": {
}
}
Then from the terminal you can use npm start
Nodemon installation: https://www.npmjs.com/package/nodemon
Solution 3 - node.js
I have a TypeScript file called "server.ts", The following npm scripts configures Nodemon and npm to start my app and monitor for any changes on TypeScript files:
"start": "nodemon -e ts --exec \"npm run myapp\"",
"myapp": "tsc -p . && node server.js",
I already have Nodemon on dependencies. When I run npm start
, it will ask Nodemon to monitor its files using the -e
switch and then it calls the myapp
npm script which is a simple combination of transpiling the typescript files and then starting the resulting server.js. When I change the TypeScript file, because of -e
switch the same cycle happens and new .js files will be generated and executed.
Solution 4 - node.js
I use Nodemon version 1.88.3 in my Node.js project. To install Nodemon, see in https://www.npmjs.com/package/nodemon.
Check your package.json, see if "scripts" has changed like this:
"scripts": {
"dev": "nodemon server.js"
},
server.js
is my file name, you can use another name for this file like app.js
.
After that, run this on your terminal: npm run dev
Solution 5 - node.js
Use -exec
:
"your-script-name": "nodemon [options] --exec 'npm start -s'"
Solution 6 - node.js
In package json
:
"scripts": {
"start": "node index",
"dev": "nodemon index"
},
"devDependencies": {
"nodemon": "^2.0.2"
}
And in the terminal for developing:
npm run dev
And for starting the server regularly:
npm start
Solution 7 - node.js
First change your package.json file,
"scripts":
{
"start": "node ./bin/www",
"start-dev": "nodemon ./app.js"
},
After that, execute command
npm run start-dev
Solution 8 - node.js
In package.json file. change file like this
"scripts":{
"start": "node ./bin/www",
"start-dev": "nodemon ./app.js"
},
and then execute npm run start-dev
Solution 9 - node.js
Nodemon emits events upon every change in state; start, restart crash, etc. You can add a Nodemon configuration file (nodemon.json) like so:
{
"events": {
"start": "npm run *your_file*"
}
}
Read more in Nodemon events — run tasks at server start, restart, crash, exit.
Solution 10 - node.js
Add this to script object from your project's package.json file
"start":"nodemon index.js"
It should be like this
"scripts": {
"start":"nodemon index.js"
}
Solution 11 - node.js
I simply use 'npx' in the terminal to set up nodemon and execute it
npx nodemon
Solution 12 - node.js
You can also install nodemon globally for frequent use:
npm i nodemon -g
or sudo npm i nodemon -g
then edit your package.json:
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js"
},
Generally, 'dev' specifies developmental use (npm run dev).
Solution 13 - node.js
It will depend on types of your Nodemon installation. If you install Nodemon globally by using commands (npm install nodemon --global
or npm install nodemon -g
), you do not have to specify any script for Nodemon in your package.json file. Just executing command nodemon index.js
will run your project.
But if you install Nodemon locally by command npm install nodemon
then you have to specify the script. If you name it as start then npm run start
or npm start
will trigger the server to run.
// Absolutely no need for global installation
"scripts": {
"start": "nodemon index.js"
}
Solution 14 - node.js
If globally installed then
"scripts": {
"start": "nodemon FileName.js(server.js)",
},
Make sure you have installed nodemon
globally:
npm install -g nodemon
Finally, if you are a Windows user, make sure that the security restriction of the Windows PowerShell is enabled.
Solution 15 - node.js
{ "name": "backend", "version": "0.0.0", "private": true, "scripts": { "start": "nodemon ./bin/www" }, "dependencies": { "bcrypt": "^5.0.1", "cookie-parser": "~1.4.4", "debug": "~2.6.9", "express": "~4.16.1", "hbs": "^4.1.2", "http-errors": "~1.6.3", "morgan": "~1.9.1", "nodemon": "^2.0.12" } }
>use "nodemon ./bin/www" scripts > start
-
eg:
> "scripts": { "start": "nodemon ./bin/www" },
Solution 16 - node.js
If you have nodemon
installed globally, simply running nodemon
in your project will automatically run the start
script from package.json
.
For example:
"scripts": {
"start": "node src/server.js"
},
From the nodemon documentation:
> nodemon will also search for the scripts.start property in package.json (as of nodemon 1.1.x).
Solution 17 - node.js
I know it's 5 years late, if you want to use nodemon.json
you may try this,
{
"verbose": true,
"ignore": ["*.test.js", "fixtures/*"],
"execMap": {
"js": "electron ." // 'js' is for the extension, and 'electron .' is command that I want to execute
}
}
The execMap
will execute like a script
in package.json, then you can run nodemon js
Solution 18 - node.js
You can use this instead of npm start :
npx env-cmd nodemon
Solution 19 - node.js
Try this, with watch:
nodemon --exec ts-node pathtoapp/filewithserver.ts -e ts
my project example: nodemon --exec ts-node src/server.ts -e ts
Solution 20 - node.js
To avoid a global install, add Nodemon as a dependency, then...
package.json
"scripts": {
"start": "node ./bin/www",
"start-dev": "./node_modules/nodemon/bin/nodemon.js ./bin/www"
},