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?
-
Funny to come back to this question almost 10 years later! I've recently authored github.com/gajus/turbowatch for the exact use case. It is worth noting that Node.js also has native --watch functionality as of v18 nodejs.org/en/blog/release/v18.11.0– GajusCommented May 17, 2023 at 15:08
10 Answers
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
-
same here, for whatever reason, as I used nodemon before without any issue. Commented Jan 1, 2014 at 11:28
-
1Worked 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.– scipilotCommented Apr 26, 2015 at 3:47 -
6
-
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.– EduardoCommented Oct 8, 2017 at 5:22
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
-
Any idea why Node requires restart to implement code changes? And why do other servers (like Apache/PHP) don't require restart? 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. Commented Dec 5, 2016 at 4:49
-
2Not 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. 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. Commented Apr 19, 2020 at 2:11
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
-
+1 forever is pretty versatile for both development/testing and production. Commented Feb 7, 2014 at 19:33
-
1I 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.– hydeCommented Dec 13, 2015 at 22:06
-
1@hyde It wasn't a delay that was required it was the
-t
orkillTree
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 -
2Forever 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/…– pmontCommented Apr 26, 2016 at 19:04
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.
-
3@2019 nodemon seems the only one of the 4 development-options that is still maintained.– mvermandCommented 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.– KayCommented Feb 18, 2019 at 20:10
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
-
1
-
How is this answer any different from the 2012 nodemon answer? Commented Apr 17, 2020 at 23:45
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.
-
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).– LitzerCommented Nov 17, 2022 at 12:05
-
-
@Mitya what is the output if you run
node -v
? Do you specify file path?– mbelskyCommented 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– MityaCommented 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.– mbelskyCommented Mar 8, 2023 at 21:06
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
console out put
-
1
Follow the steps:
npm install --save-dev nodemon
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"
}
npm run devstart
https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/skeleton_website
-
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
-
2
-
3This 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. (Fortunatelyrun
hasn't been updated since 2015, so now it's not much of an option.) Commented Apr 17, 2020 at 23:48
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. 😃🚀