90

I am using create-react-app and hosting in its default port localhost:3000 and want to access this from another device on the same network.

I got the IP of my host's IP (using ifconfig) 192.168.0.5 and tried opening 192.168.0.5:3000 but that did not work.

Is there any way to achieve this?

19 Answers 19

105

Simply run HOST=0.0.0.0 npm run start.
Afterwards open the url from another device on the network.

In your case, 192.168.0.5:3000 would work.

Documentation for setting HOST environment variables.

8
  • That's awesome Elad, Thanks!
    – oriharel
    Jan 29, 2019 at 10:20
  • 15
    Solved, If you're using webpack, Open package.json add --host 0.0.0.0 option to script.start field. Done.
    – kitta
    Apr 25, 2019 at 19:26
  • I just get blank white page, is there a way to debug whats wrong?
    – Lance
    Jun 30, 2020 at 17:05
  • 1
    [MacOS] If you don't know your IP, go System Preferences > Network > Advanced > TCP/IP and check the IPv4 Address
    – DeepBlue
    Oct 12, 2020 at 18:25
  • 2
    You might not actually need the HOST=0.0.0.0 part. In my case, running the normal start command (npm run start) worked. I was able to see the app on my phone's browser at URL [My Macbook's IP address]:3000. I used this to find my macbook's IP address. The docs in your post mention this default behavior, under the HOST section. Nov 10, 2021 at 0:58
20

If you are on Ubuntu, simply run

$> sudo ufw allow 3000

Then access your app using your internal ip from local network. to get your local IP run:

$> ifconfig
2
20

I had the same problem and when I disabled the private firewall it worked for me.

enter image description here

or if you want to enable private firewall you need to go here:

Control Panel\System and Security\Windows Defender Firewall\Allowed applications

enter image description here

and enable "change settings" than check private checkbox in Node.js row

enter image description here

1
  • 1
    Thank you, It worked fine. But is there any security issues??
    – Faith Sabu
    Nov 17, 2022 at 16:21
17

As I can't post comment, In complementary to Elad if you have react-scripts start instead of npm run start

HOST=0.0.0.0 react-scripts start

Works too !

4

In my case, npm run start used my Ethernet adapter's IP e.g. http://192.168.167.113:3000 but as I was accessing the site using WLAN, I needed to use WLAN IP which was 192.168.0.227.

Make sure to use WLAN IP with the same port to make it work.

4

If you are on windows and the above solutions don't work, it is most probably a firewall issue. You can try looking in Allowed Applications in Control Panel, to check if node is allowed on private networks. Or looking in the Windows Defender Firewall with Advanced Security and checking the Monitoring\Firewall tab and check for Node.js.

3
  • 1
    Also, I found the output of the npm run start was printing a misleading IP address. I had to check my ip via ipconfig .Once using the proper IP address my issue was resolved. Dec 27, 2020 at 0:19
  • Thanks for your comment. I had the same problem, But Why do you think npm run starts to print a misleading IP address? I had to change to the correct IP address and it worked
    – Dijiflex
    Jun 30, 2021 at 10:24
  • It totally was! Thank you!
    – guilfer
    Oct 31, 2021 at 17:10
4

I am using ubuntu 20.04 and both my android and laptop was connected to same wifi network.

Step 1: in package.json set --host=0.0.0.0 for server key.


...

  "scripts": {
    "go": "open http://localhost:3000; npm run server",
    "e2e": "nightwatch --config nightwatch.js",
    "run-e2e": "concurrently -s first -k \"npm start\" \"sleep 15; npm run e2e\"",
    "test": "./node_modules/.bin/concurrently -k 'npm run server' 'npm run e2e'",
    "start": "npm run server",
    "server": "live-server public --host=0.0.0.0 --port=3000 --middleware=./disable-browser-cache.js"
  },

...

Step 2: Add rule for port 3000 in ubuntu firewall.

sudo ufw allow 3000

Step 3: Now I can access same react app on

  • http://localhost:3000/ # desktop
  • http://127.0.0.1:3000/ # desktop
  • http://192.168.X.X:3000/ # from android and desktop both (replace with your internal IP)
2
  • Does it makes a difference if one is on wifi and the other on cable? Since one is 10.0.0.101 and the other is 10.0.0.107? Apr 19, 2021 at 14:48
  • Until both device are on same network cable and wifi is same. Check internal IP (in ubuntu I use ifconfig command). If both device can access this internal IP (mine was 192.168.1.5) then There is no issue. Only possible if cable comes from WiFI router and mobile connected to same WiFi (you case possibly) Apr 19, 2021 at 19:08
2

at the end of 2021 was doing several react projects via tutorials at that time when i started apps, it was showing 2 links with localhost and ip address link and 2nd link was for devices it the same network. and now (18 may 2022) when i wanted to make react app independently, when i am starting my react app its giving link with localhost. in my case (windows 11) try http://yourComputerIp:portNumber

to get ip on windows type ipconfig in cmd and your ip address will be under name IPv4 Address

1

if you have two machines (lets say pc1 and pc2) and both are connected over the same wifi connection, then:

  • bind your create-react-app development server to your Wireless LAN adapter wi-fi IPv4 address (on windows type the command ipconfig.exe and unix-like systems type ifconfig).. you will find the ip address under something:
  • Wireless LAN adapter Wi-Fi (on windows)
  • wlp5s0 inet Ip_Address
  • in your sheel where you develop react run HOST=your_IP npm run start

now, your development machin will be the server that serves the ui over it's IP_Address

1
  • Thanks this was the only answer that helped me. :)
    – llsanketll
    Dec 14, 2020 at 11:14
1

Sometimes there is another problem for Windows users.

Windows Firewall blocks NodeJs.

Just go to Windows Defender Firewall and click to Allow an app or feature through Windows Defender Firewall and allow Node.js

1

If you are facing

HOST=0.0.0.0 : The term 'HOST=0.0.0.0' is not recognized as the name of a cmdlet

If I run (without using the npm wrapper script)

HOST=0.0.0.0 PORT=8000 ./node_modules/.bin/react-scripts start

To fix it, go to package.json and changed the "start" script to

"start": "./node_modules/.bin/react-scripts start",

then add this line of code.

"server": "live-server public --host=0.0.0.0 --port=3000 --middleware=./disable-browser-cache.js",

Then npm start works fine.

more details link

2
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – m4n0
    Sep 24, 2021 at 4:46
  • Thanks for pointing it out. I'll be careful in the future on this.
    – Amit Kundu
    Sep 24, 2021 at 6:04
1

As someone else already mentioned, it's possible that React is just giving you the wrong IP address. In my case, React was pulling from one of my VMWare ethernet adapters. To open the right one, type ipconfig in Command Prompt and use the IPv4 Address listed under "Wireless LAN adapter Wi-Fi" or your actual ethernet adapter.

0

Can you please turn off your firewall and check 192.168.0.5:3000.

Thanks

1
  • This is not correct as provided IP might be correct for you but not for others depending on whe WAN ip address Jun 27, 2022 at 17:03
0

After running npm start go to your terminal "bash, cmd ..etc" and run ipconfig

IPv4-Adresse ..... : for example 111.222.333.444

open browser in your second device and type : 111.222.333.444:portNumber

0

In my case, Windows Firewall blocked port 3000. So adding an Inbound Rule to open port 3000 did the trick.

Windows Defender Firewall with Advanced Security > Inbound Rules > New Rule...

0
0

If you are using windows then

  1. run this echo ((ipconfig | findstr [0-9].\.)[0]).Split()[-1] on your powershell or directly on the vscode terminal.
  2. then do npm start and access your react-app using the ip shown by the above step ( yourIp:port , e.g. 192.168.8.100:3000)
0

Step:1 - In webpack.config.js change host to "host: '0.0.0.0'", Step:2 - npm run start, Step:3 - run ipconfig and replace your ipv4 with http://192.168.X.X:3000,

In case it won't work add allow Nodejs in firewall and add Inbound rule with port 3000

1
  • 1
    Please provide an explanation of your answer so that the next user knows why this solution worked for you.
    – Elydasian
    Jul 21, 2021 at 8:34
0

My react-scripts:3.4.1 is working,

from

"scripts": {
  "start": "react-scripts start"
}

to ( 0.0.0.0 is not work )

"scripts": {
  "start": "HOST=127.0.0.1 react-scripts start"
}
0

if you are using an azure VM make sure you add an inbound rule to allow traffic for port 3000 you can do this by

  1. go to your deployed VM
  2. go to networking
  3. and add an Allow inbound rule for port 3000

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.