When I run a nodejs server with the following command:
"start": "nodemon --max-old-space=8192 ./src/app.js --exec babel-node"
and I change anything in the code, nodemon automatically reloads the code and restarts the server with the following message.
[nodemon] restarting due to changes...
[nodemon] starting `babel-node --max-old-space=8192 ./src/app.js`
How do I restart the server manually the same way?
Or in other words: What do I write in the package.json scripts "restart" command to simulate the same behaviour that is done by nodemon automatically?
Thanks
Press Ctrl + C to exit from Nodemon on windows. If that does not work, simply end the task from task manager and run it again.
Use npx to solve the error "'nodemon' is not recognized as an internal or external command, operable program or batch file", e.g. npx nodemon server. js or install the package globally by running npm install -g nodemon and make sure your PATH environment variable is set up correctly.
js Automatic restart Node. js server with nodemon. In this case, if we make any changes to the project then we will have to restart the server by killing it using CTRL+C and then typing the same command again.
As stated in the documentation, you can restart manually by typeing rs
in the console where nodemon
is running.
There is no external command to trigger a restart from a different process.
One workaround would be to trigger a restart by simulating a change of a file.
A simple touch
on a watched file is enough. So you could write a npm script that touches one of the watched files.
"restart": "touch app.js"
The purpose of nodemon
is to listen changes of the file and restart the server. If you want manually to restart the server then you no need to use nodemon, you can use just node
command.
The below code would serve the purpose.
{
"scripts": {
"start": "node ./src/app.js",
"restart": "kill -9 $(ps aux | grep '\snode\s' | awk '{print $2}') && node ./src/app.js "
},
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With