How to open a create-react-app from another computer connected to the same network?
Asked Answered
R

22

99

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?

Runnymede answered 21/11, 2017 at 11:46 Comment(0)
U
117

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.

Unlearn answered 25/12, 2018 at 10:11 Comment(7)
Solved, If you're using webpack, Open package.json add --host 0.0.0.0 option to script.start field. Done.Paroicous
I just get blank white page, is there a way to debug whats wrong?Polystyrene
[MacOS] If you don't know your IP, go System Preferences > Network > Advanced > TCP/IP and check the IPv4 AddressArlyn
how can we replace system IP address to localhost?Dipterous
It is better to set HOST=127.0.0.1, because I always get warnings when HOST=0.0.0.0Cote
How to change port from 3000 to differentCalais
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.Motherland
P
25

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

Pina answered 9/9, 2021 at 12:53 Comment(1)
Thank you, It worked fine. But is there any security issues??Redroot
S
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
Scuppernong answered 10/7, 2020 at 19:42 Comment(2)
What about windows users?Coenocyte
@Coenocyte Add a firewall rule to allow port 3000. To know how to add a firewall rule read https://mcmap.net/q/218471/-windows-firewall-open-portLes
A
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 !

Astray answered 28/1, 2019 at 17:10 Comment(0)
T
5

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)
Tempura answered 13/4, 2021 at 10:37 Comment(3)
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?Kakalina
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)Tempura
Working as a charmContrasty
M
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.

Manisa answered 23/4, 2020 at 16:21 Comment(0)
E
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.

Emotionalize answered 14/6, 2020 at 12:1 Comment(3)
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.Exasperate
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 workedHelminthology
It totally was! Thank you!Metempsychosis
U
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

Undulation answered 18/5, 2022 at 12:37 Comment(0)
H
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

Hawes answered 29/10, 2020 at 12:43 Comment(1)
Thanks this was the only answer that helped me. :)Sites
W
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

Whitleywhitlock answered 18/1, 2021 at 12:33 Comment(0)
I
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

Interclavicle answered 23/9, 2021 at 15:56 Comment(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 ReviewAsshur
Thanks for pointing it out. I'll be careful in the future on this.Interclavicle
L
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.

Laundes answered 10/12, 2021 at 18:4 Comment(0)
M
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"
}
Milium answered 18/6, 2020 at 15:1 Comment(0)
H
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

Hembree answered 5/12, 2020 at 17:58 Comment(0)
P
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...

Palmation answered 8/3, 2021 at 17:51 Comment(0)
S
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)
Spring answered 5/7, 2021 at 5:19 Comment(0)
D
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

Darnel answered 21/7, 2021 at 5:59 Comment(1)
Please provide an explanation of your answer so that the next user knows why this solution worked for you.Heartwarming
C
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
Cereal answered 3/12, 2021 at 16:34 Comment(0)
P
0

if you are on windows ,please turn off your firewall and replace your address on the phone with the Wireless lan adapter WIFI ipv4 address which you can find it easily by typing ipconfig on PowerShell.

Perfectly answered 22/4, 2023 at 19:16 Comment(0)
E
0

You must connect the same network while serving with other systems, then you can simply enter:

npm start HOST="current_ip_address"

(ex: 191.161.221.209) and share the ip_address with port number

Elson answered 16/8, 2023 at 9:44 Comment(0)
B
0
npm run start --host=0.0.0.0

You can try using the host value using the flag and once compiled, yu will get the url to access within the network and local as well

Betulaceous answered 20/9, 2023 at 12:2 Comment(0)
H
-1

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

Thanks

Hardpressed answered 21/11, 2017 at 12:20 Comment(1)
This is not correct as provided IP might be correct for you but not for others depending on whe WAN ip addressCaphaitien

© 2022 - 2024 — McMap. All rights reserved.