How to properly restart nodemon server
Asked Answered
R

6

9

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

Rag answered 11/4, 2018 at 9:52 Comment(2)
Typing 'rs' in the console?Pelvic
I was searching how to execute a command/callback after Nodemon start/restart and you helped me a lot :) Thanks!Bisque
T
5

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 "
    },
}
Truculent answered 11/4, 2018 at 10:49 Comment(0)
H
13

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"
Heddie answered 11/4, 2018 at 10:8 Comment(0)
T
5

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 "
    },
}
Truculent answered 11/4, 2018 at 10:49 Comment(0)
A
2

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.

Antihalation answered 16/7, 2022 at 17:12 Comment(1)
I love this fix, simple and easy to use :)Ecclesiolatry
B
2

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

Bethelbethena answered 5/11, 2022 at 22:41 Comment(0)
H
-1

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>
Hanni answered 29/3, 2019 at 5:2 Comment(0)
P
-1

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.

Plasia answered 8/8, 2020 at 14:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.