Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restart node upon changing a file

Tags:

node.js

People also ask

How do I restart node server automatically?

Running non-Node code While Nodemon is running, we can manually restart our application. So instead of stopping and restarting Nodemon, we can just type rs and press enter, and Nodemon will restart the server or the running process for us.

Which command will you run to restart your node server after making some changes?

You can restart the process at any time by typing rs and hitting ENTER . Once you make the changes to package. json , you can then call nodemon to start the example app in watch mode without having to pass in server.

Does node require restart?

During the Node. js development process, developers are required to make several changes in the running code. To make the updates take effect, the Node. js server needs to be restarted repeatedly.


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

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.

Example:

nodemon ./server.js localhost 8080

or simply

nodemon server

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

Various NPM packages are available to make this task easy.

For Development

  • nodemon: most popular and actively maintained
  • forever: second-most popular
  • node-dev: actively maintained (as of Oct 2020)
  • supervisor: no longer maintained

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

  • pm2: npm install -g pm2
  • Strong Loop Process Manager: npm install -g strongloop

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


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

node-dev

node-dev is great alternative to both nodemon and supervisor for developers who like to get growl (or libnotify) notifications on their desktop whenever the server restarts or when there is an error or change occur in file.

Installation:

npm install -g node-dev

Use node-dev, instead of node:

node-dev app.js

Notification on Changing file so server start automatically

enter image description here

console out put

enter image description here


I use runjs like:

runjs example.js

The package is called just run

npm install -g run