KasparTr
KasparTr

Reputation: 2458

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

Upvotes: 9

Views: 21594

Answers (6)

Abolfazl Roshanzamir
Abolfazl Roshanzamir

Reputation: 14892

Say goodbye to nodemon.

Node v18.11.0

Running in 'watch' mode using node --watch restarts the process when an imported file is changed.

try as follow:

node --watch app.js

For more information

Upvotes: 3

dansch
dansch

Reputation: 6267

Tried a few things to restart nodemon from within a running script.

var fs = require('fs');
fs.utimesSync(__filename, Date.now(), Date.now());

This will touch the current file, which should trigger a restart if nodemon is watching.

Upvotes: 2

8Observer8
8Observer8

Reputation: 1172

Source: https://www.npmjs.com/package/nodemon

Manual restarting


Whilst nodemon is running, if you need to manually restart your application, instead of stopping and restart nodemon, you can type rs with a carriage return, and nodemon will restart your process.

Upvotes: -1

Amirsalar
Amirsalar

Reputation: 729

If you are specifically looking to resolve "listen EADDRINUSE: address already in use" error after a while, you can check what application is using the port that nodemon wants to use:

sudo lsof -i :4500

The above will give you the PID of the app that is using that port. Then you may kill the process by:

kill -9 <PID>

Upvotes: -1

Thavaprakash Swaminathan
Thavaprakash Swaminathan

Reputation: 6994

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 "
    },
}

Upvotes: 5

lukas-reineke
lukas-reineke

Reputation: 3322

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"

Upvotes: 13

Related Questions