210

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?

1

10 Answers 10

165

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
5
  • same here, for whatever reason, as I used nodemon before without any issue.
    – ZenMaster
    Commented Jan 1, 2014 at 11:28
  • 1
    Worked for me too. I had to override the default wildcard watch parameter to server.js because it was constantly restarting, this was due to my server building the client on boot and thus changing the files. supervisor --watch server.js server.js solved it.
    – scipilot
    Commented Apr 26, 2015 at 3:47
  • 6
    Node-supervisor isn't maintained any more.
    – aleung
    Commented Jun 6, 2015 at 9:05
  • My favourite one, refresh intervals to check if file is modified is almost instant! Commented Mar 27, 2017 at 5:54
  • There is a bug. Add a new js file and then change an existing js file to require it. Application restarts ok. Change that last added js file and application doesn't restart.
    – Eduardo
    Commented Oct 8, 2017 at 5:22
127

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
6
  • Any idea why Node requires restart to implement code changes? And why do other servers (like Apache/PHP) don't require restart?
    – darKnight
    Commented Oct 26, 2016 at 10:20
  • 2
    @dk49 because PHP scripts in that case are started for every incoming request, and stopped after they've done generating the page. Commented Dec 4, 2016 at 22:51
  • 1
    @Daerdemandt thanks for sharing that..but I still did not get why does the server (on any platform) need to start and stop? Is it because it maintains a cache of files (responses)? I thought that the server will be loading the files from disk at the time of request, process it and then send the response. I am new to server-side programming.
    – darKnight
    Commented Dec 5, 2016 at 4:49
  • 2
    Not sure how nodemon still holds up today (as this was back in 2012), but for security reasons I'm not sure I would want my server restarting on prod whenever a file changes. Something like pm2 might be more appropriate for a production environment.
    – Menztrual
    Commented Mar 6, 2017 at 22:13
  • 1
    @maverick "node" just runs whatever script you tell it to use, that is the expected behavior. In general, you wouldn't want your app to stop at some arbitrary point because a file changed, would be bad for security, uptime, etc. If you create a server like php server.php you'll see it's the same as Node, it will keep running (if you configure it right.) But Node was created from scratch to act as a server, PHP wasn't. I vaguely remember hearing Lisp can somehow recompile itself while running (disclaimer: not a Lisp dev) which made it attractive for "artificial intelligence" enthusiasts.
    – Jay Brunet
    Commented Apr 19, 2020 at 2:11
111

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
6
  • +1 forever is pretty versatile for both development/testing and production.
    – smertrios
    Commented Feb 7, 2014 at 19:33
  • 1
    I have tried using forever to restart my ES2015 Expess.js/Webpack server with this command forever -c babel-node -w --watchDirectory ./server ./server/index.js but sadly it seems to send forever into a tailspin and it restarts the server too often, resulting in port already in use errors...is there someway to add a pause after a restart? Commented Dec 13, 2015 at 22:00
  • 1
    @BrianDiPalma You should perhaps add some ignore patterns, if there are any changing files, such as logs, under the watch directory. Check the docs to see if you can define a delay between restarts.
    – hyde
    Commented Dec 13, 2015 at 22:06
  • 1
    @hyde It wasn't a delay that was required it was the -t or killTree option that was required, I guess babel-node was being killed but not the Express server it spawned. Commented Dec 13, 2015 at 22:26
  • 2
    Forever JS has a bug in Windows that doesn't kill the Node process when the script is terminated. Have to manually kill the Node process :( stackoverflow.com/questions/14556852/…
    – pmont
    Commented Apr 26, 2016 at 19:04
60

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.

2
  • 3
    @2019 nodemon seems the only one of the 4 development-options that is still maintained.
    – mvermand
    Commented Jan 18, 2019 at 7:31
  • 2
    @mvermand Yeah, no new releases have been released recently for either one of them except for nodemon. For production, PM2 is still well maintained.
    – Kay
    Commented Feb 18, 2019 at 20:10
23

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
2
  • 1
    nodemon has annoying bugs. It was a time waster for me.
    – kta
    Commented Aug 16, 2017 at 1:15
  • How is this answer any different from the 2012 nodemon answer? Commented Apr 17, 2020 at 23:45
11

Nodejs supports watching mode since v18.11.0. To run it just pass --watch argument:

node --watch ./index.js

Note: this is an experimental feature.

5
  • Replaced nodemon in my case. If needed to restart as 'rs' command do in nodemon, just save related file in the IDE (no changes needed).
    – Litzer
    Commented Nov 17, 2022 at 12:05
  • For me this result in node: bad option: --watch
    – Mitya
    Commented Mar 8, 2023 at 12:07
  • @Mitya what is the output if you run node -v? Do you specify file path?
    – mbelsky
    Commented Mar 8, 2023 at 13:33
  • @mbelsky 16.14.2. And yes, I used node --watch ./bin/www. In the end I just installed nodemon
    – Mitya
    Commented Mar 8, 2023 at 14:32
  • @Mitya well this is the reason. --watch option is available only in node v18.11.0 and higher versions.
    – mbelsky
    Commented Mar 8, 2023 at 21:06
10

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

1
  • 1
    For TypeScript users there's ts-node-dev
    – BrunoLM
    Commented Oct 6, 2020 at 13:19
7

Follow the steps:

  1. npm install --save-dev nodemon

  2. Add the following two lines to "script" section of package.json:

"start": "node ./bin/www",
"devstart": "nodemon ./bin/www"

as shown below:

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node ./bin/www",
    "devstart": "nodemon ./bin/www"
}
  1. npm run devstart

https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/skeleton_website

1
  • Well, it's essentially the same answer as the 2012 one, but present with more "magic" around it. But the essence of the answer is "use nodemon". The reset is tangential. And it was poorly formatted, but I see that David fixed that, so I'll undo my downvote. Commented Apr 28, 2020 at 5:47
3

I use runjs like:

runjs example.js

The package is called just run

npm install -g run
2
  • 2
    it's not better, just another option
    – sp2danny
    Commented Nov 10, 2015 at 12:01
  • 3
    This answer would be much more valuable if it explained why you use run, and how it compared to existing alternatives. Just dumping another option only contributes to the paradox of choice. (Fortunately run hasn't been updated since 2015, so now it's not much of an option.) Commented Apr 17, 2020 at 23:48
0

I'm a newbie to NodeJS. I tried this method, and it works well and is easier for me.

npm install --save nodemon

update the package.json script Before

"scripts": {
  "test": "echo \"Error: no test specified\" && exit 1"
},

After

"scripts": {
  "start": "nodemon index.js"
},

and then

npm start

I hope it's as easy for me as it is for you. 😃🚀

Not the answer you're looking for? Browse other questions tagged or ask your own question.