I\'ve installed and is running a node.js server on osx. I\'ve dled a chat module and is happily running it. I\'ve altered some pieces and need to restart the server to see t
To say "nodemon" would answer the question.
But on how only to kill (all) node demon(s), the following works for me:
pkill -HUP node
If I am just run the node app from console (not using forever etc) I use control + C, not sure if OSX has the same key combination to terminate but much faster than finding the process id and killing it, you could also add the following code to the chat app you are using and then type 'exit' in the console whenever you want to close down the app.
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(data) {
if (data == 'exit\n') process.exit();
});
I understand that my comment relate with windows, but may be someone be useful. For win run in cmd:
wmic process where "commandline like '%my_app.js%' AND name='node.exe' " CALL Terminate
then you can run your app again:
node my_app.js
Also you can use it in batch file, with escape quotes:
wmic process where "commandline like '%%my_app.js%%' AND name='node.exe' " CALL Terminate
node my_app.js
At first open terminal/command line then go to your project directory, now install nodemon by using command npm install nodemon --save-dev this command will make sure it saved as developer dependency. If you are working with expressjs then in your package file it will look like
{
"name": "expressjs-app",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"express": "~4.16.1",
"http-errors": "~1.6.3",
"morgan": "~1.9.1",
"pug": "^2.0.4"
},
"devDependencies": {
"nodemon": "^2.0.3"
}
}
now modify the "start" value in your package.json file, for production we will use the exsiting value but for development will use nodemon to track the changes in source file without restarting server. There for new value for start is "start": "if [[$NODE_ENV=='production']]; then node ./bin/www; else nodemon ./bin/www; fi"
final package.json file will look like
{
"name": "expressjs-app",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "if [[$NODE_ENV=='production']]; then node ./bin/www; else nodemon ./bin/www; fi"
},
"dependencies": {
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"express": "~4.16.1",
"http-errors": "~1.6.3",
"morgan": "~1.9.1",
"pug": "^2.0.4"
},
"devDependencies": {
"nodemon": "^2.0.3"
}
}
to uninstall nodemon jusy simply run the command npm uninstall nodemon
Using "kill -9 [PID]" or "killall -9 node" worked for me where "kill -2 [PID]" did not work.
During development the best way to restart server for seeing changes made is to use nodemon
npm install nodemon -g
nodemon [your app name]
nodemon will watch the files in the directory that nodemon was started, and if they change, it will automatically restart your node application.
Check nodemon git repo: https://github.com/remy/nodemon