node.js - Restart node upon changing a file

ID : 20082

viewed : 3

Tags : node.jsnode.js

Top 5 Answer for node.js - Restart node upon changing a file

vote vote


A good option is Node-supervisor:

npm install supervisor -g 

and after migrating to the root of your application use the following

supervisor app.js 
vote vote


You should look at something like nodemon.

Nodemon will watch the files in the directory in which nodemon was started, and if they change, it will automatically restart your node application.


nodemon ./server.js localhost 8080 

or simply

nodemon server 
vote vote


forever module has a concept of multiple node.js servers, and can start, restart, stop and list currently running servers. It can also watch for changing files and restart node as needed.

Install it if you don't have it already:

npm install forever -g 

After installing it, call the forever command: use the -w flag to watch file for changes:

forever -w ./my-script.js 

In addition, you can watch directory and ignore patterns:

forever --watch --watchDirectory ./path/to/dir --watchIgnore *.log ./start/file 
vote vote


Various NPM packages are available to make this task easy.

For Development

For Production (with extended functionality such as clustering, remote deploy etc.)

Comparison between Forever, pm2 and StrongLoop can be found on StrongLoop's website.

vote vote


You can also try nodemon

To Install Nodemon

npm install -g nodemon 

To use Nodemon

Normally we start node program like:

node server.js 

But here you have to do like:

nodemon server.js 

Top 3 video Explaining node.js - Restart node upon changing a file