So, I'm in the process of learning node, and am finding that for testing, I have to kill the running instance of node, then start it up again with the new application code for the server. So, I first execute my node server like this: node myapp.js
.
Then, I notice something isn't working right, or I want to add some new code to myapp.js. I go into Process Explorer (I'm running node on a Windows box), and kill the node.exe process, and then upload the new myapp.js file with the changes. Then I do node myapp.js
to start it again to test again.
There has got to be an easier way to do this. I typed in node --help
but there are too many flags listed to see if there is some kind of restart mechanism similar to apache on linux like: httpd -k restart
. Is there a kind of "graceful" restart with node?
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.
If it's just running (not a daemon) then just use Ctrl-C . Where PID is replaced by the number in the output of ps . You could also use "killall -2 node", which has the same effect.
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.
In this case, if we make any changes to the project then we will have to restart the server by killing it using CTRL+C and then typing the same command again.
Check out nodemon. Install it using npm install nodemon
, after that simply use nodemon server.js
if server.js is the file you're working on.
Use node-supervisor
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With