Skip to content

Instantly share code, notes, and snippets.

@artginzburg
Last active April 17, 2022 00:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artginzburg/e50f99bed010d71551ddba9c8e62c479 to your computer and use it in GitHub Desktop.
Save artginzburg/e50f99bed010d71551ddba9c8e62c479 to your computer and use it in GitHub Desktop.
Something is already running on port 6969

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 uses 0.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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment