Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly restart nodemon server

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

like image 783
KasparTr Avatar asked Apr 11 '18 09:04

KasparTr


People also ask

How do I shutdown a Nodemon server?

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.

How do I fix Nodemon is not working?

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.

How do I restart a node project?

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.


2 Answers

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"
like image 180
lukas-reineke Avatar answered Oct 15 '22 23:10

lukas-reineke


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 "
    },
}
like image 42
Thavaprakash Swaminathan Avatar answered Oct 16 '22 01:10

Thavaprakash Swaminathan