Problem
Have you ever encountered something like Error: listen EADDRINUSE: address already in use :::3000
when your app is starting?
After which you have to change the port or shutdown whatever is running on the already used one...
Solution
npm i react-dev-utils
import { choosePort } from 'react-dev-utils/WebpackDevServerUtils'
Wherever you need to use the port, just use const port = await choosePort(HOST, PORT)
instead.
If you're sure you'll be running on localhost, you can set HOST to whatever, like
undefined
. React uses0.0.0.0
afaik.
+const port = await choosePort('0.0.0.0', PORT)
+http.listen(port, function () {
-http.listen(PORT, function () {
console.log(`App started!`)
});
Result
On your app start, if the port is taken, the terminal will simply ask you to approve port changing. Press Enter and you're done.