If I enter commmand webpack-dev-server the webpack server will obviously start. But If I type that commnad again, I got error, because an instance the server is running and it's taking the locahost port.
Is there way to config webpack dev server so that it kill whatever instance of it before it starts?
Running killall node before restarting usually does the trick for me.
like this?
scripts : {
"dev" : "killall node && webpack-dev-server"
}
not working. error No matching processes belonging to you were found
When you're running killall node and there's nothing to kill, that's what it will return. I wouldn't recommend adding it to an npm script, just run it as needed, or have a script to run it only if there are node processes running that need to be killed.
If you search on "find and kill process in bash" on Google you'll find a lot of answers on how to do this. This StackOverflow question for example. From a quick test, pkill -f webpack-dev-server; webpack-dev-server works. Note the use of ; instead of &&, because pkill will return a non-zero exit-code if it can't find any processes.
Edit: if you want to be sure that you only kill the webpack-dev-server process of the current project, use pkill -f project_folder/node_modules/.bin/webpack-dev-server
Most helpful comment
Running
killall nodebefore restarting usually does the trick for me.