question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow running two apps on 127.0.0.1 and another address on same port

See original GitHub issue

Describe the bug

Currently, when starting two React apps with e.g. HOST=127.0.0.1 PORT=3000 and HOST=127.2.2.2 PORT=3000, the second app will fail with an error Something is already running on port 3000, although starting two apps on same port but different addresses is valid. Note that this works when not using address 127.0.0.1.

Did you try recovering your dependencies?

N/A

Environment

N/A

Steps to reproduce

  1. Create a React app
  2. Run HOST=127.0.0.1 PORT=3000 npm run start in a first terminal
  3. Run HOST=127.2.2.2 PORT=3000 npm run start in a second terminal

Expected behavior

Both apps are running on their respective address.

Actual behavior

Only the first app (on 127.0.0.1:3000) is running, the second app didn’t start with an error: Something is already running on port 3000.

When patching the code manually to bypass this check, both apps can run aside without issues.

Reproducible demo

Any React app is applicable.

Trivia

The issue is coming from the package detect-port-alt from @Timer, a fork of detect-port which adds host support, but doesn’t cover this edge case. Unfortunately, this fork doesn’t allow creating GitHub issues and changes have not been upstreamed.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
rv2673commented, May 21, 2022

The upstream has gotten support for hostname, it only has a slightly different interface. Replacing the fork(detect-port-alt) with the upstream(detect-port) one might solves this. It checks if the port is free for the given hostname and port combination if caller provides hostname instead of checking all localhost, default host, ip combinations. Which I believe is what the call for detect is trying to do in react-dev-utils/WebpackDevServerUtils.js.

So aside from replacing the dependency, the invocation of detect needs to change from:

function choosePort(host, defaultPort) {
  return detect(defaultPort, host).then(
function choosePort(host, defaultPort) {
  return detect({port: defaultPort, hostname: host}).then(
1reaction
krreetcommented, May 20, 2022

Hi , I tried to reproduce this issue

Steps to reproduce

  1. Create a React app
  2. Run HOST=127.0.0.1 PORT=3000 npm run start in a first terminal
  3. Run HOST=127.2.2.2 PORT=3000 npm run start in a second terminal

you get an error , However if we swap step 2 and step 3 , then there is no error

Steps to reproduce

  1. Create a React app
  2. Run HOST=127.2.2.2 PORT=3000 npm run start in a first terminal
  3. Run HOST=127.0.0.1 PORT=3000 npm run start in a second terminal

I have looked into the detect-port-alt code. and found that regardless of the host it always check that port against localhost.

here https://github.com/Timer/detect-port/blob/master/lib/detect-port.js line no 68

image

based on above reasoning lets try to explain the behavior 2. Run HOST=127.0.0.1 PORT=3000 npm run start in a first terminal ===> this causes localhost 3000 to be blocked. because 127.0.0.1 is localhost 3. Run HOST=127.2.2.2 PORT=3000 npm run start in a second terminal ===> this causes localhost 3000 not to be blocked because 127.2.2.2 is not localhost

hope this helps

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can two applications listen to the same port? - Stack Overflow
For TCP, no. You can only have one application listening on the same port at one time. Now if you had 2 network...
Read more >
Running Multiple IIS Websites on the Same Port or IP Address
Let's suppose that you have a website running on IIS and listening 80 port. And you need to bind second website to the...
Read more >
Is the Nth ports on 127.0.0.1 and 127.0.0.2 same
Yes, you can use the same port number on two different 127.X.X.X addresses. Are these ports same or can I use them separately...
Read more >
Multiple application on single PC [closed] - Server Fault
What is the best practice for running multiple server on same server? ... With same IP address but different port (127.0.0.1:8080, ...
Read more >
Part 2.6 - Run two ASP.NET Core applications at the same time
NET Core applications run side-by-side, listen on different ports, ... to bind to address http://127.0.0.1:5000 : address already in use.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found