Reputation: 484
Every time i switch branches on git, there is a build error. The build disappears if I restart npm. How to automatically restart npm every time i switch branches on git? I tried nodemon, but it restarts way too many times. Is there any other solution that works for this specific situation?
Upvotes: 1
Views: 1455
Reputation: 11739
If you want to run a specific command after switching between git branches (in your case npm restart
), you might want to create git alias and execute restart immediately after the checkout. Just add the following to your git config file.
[alias]
npm-checkout = "!res() { git checkout $@ && npm restart; }; res"
So from now on instead of using git checkout
you will use git npm-checkout
which will switch git branch and restart npm.
============================== Updated ====================================
It will work only if you run git npm-checkout
from the same terminal. However if you want to restart node which is running in a different terminal, there are a few additional steps. One of the possible solutions is assign an process id to your app, and then kill it using linux pkill
command.
`
app.js:
process.title = "processId";
console.log("Sleep for 10 seconds");
setTimeout(function () {
console.log("Wake up")
}, 10000);`
and inside your package.json
`
{
"name": "test",
"main": "app.js",
"scripts": {
"start": "node app.js",
"stop": "pkill processId || true"
}
}
`
I hope this helps.
Upvotes: 1