node.js

Restart node upon changing a file


For someone who is coming from PHP background the process of killing node and starting it again after every code change, seems very tedious. Is there any flag when starting a script with node to automatically restart node when code change is saved?


Solution

  • 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