2

I am writing a serve program in node.js and I want to be able to restart it from within the code. I would like the variables to be cleared and event listeners to be ended. How would I do that?

Someone
  • 800
  • 1
  • 7
  • 25
  • 3
    Possible duplicate of [node.js app that can restart itself](https://stackoverflow.com/questions/9357757/node-js-app-that-can-restart-itself) – falinsky Nov 06 '18 at 15:39
  • assuming you are running this on linux - you can write a shell script to get the PID assigned to your running app, and then stop the app and start it again. call this shell script from within your application when required to restart the app. – xan_z Nov 06 '18 at 15:45
  • Please mark my answer as a possible solution, if that helped you. Thank you! – Malakai Feb 20 '19 at 14:19

1 Answers1

1

I assuming you've never heard about 'nodemon' that allows you to restart Node.Js application accordingly to changes. This can be also called "Hot swap" or "Hot reload".

If you run your application under Unix / Windows systems then you need to install an additional global package called 'nodemon'.

Linux / Windows:

npm install -g nodemon

Please note: Sometimes, the installation fails (for me particularly) on Linux even tho console returns "Success" installation.

I would suggest you install with admin permission like any other -g packages:

sudo npm install -g nodemon

After installation, you would be able to launch the project:

  • cd yourProject/
  • run nodemon index.js

And you are finished!

Malakai
  • 3,011
  • 9
  • 35
  • 49
  • Can I restart the program from within itself with nodemon? – Someone Nov 06 '18 at 16:23
  • What do you mean 'from within itself'? I would suggest you have a look on the official page for more info: https://nodemon.io/ Update: active discussion goes here https://github.com/remy/nodemon/issues/393 I think this will be useful for you. – Malakai Nov 06 '18 at 17:12