237

I am new to the whole nodejs/reactjs world so apologies if my question sounds silly. I am currently playing around with reactabular.js.

Whenever I do a npm start it always runs on localhost:8080.

How do I change it to run on 0.0.0.0:8080 to make it publicly accessible? I have been trying to read the source code in the above repo but failed to find the file which sets this setting.

Also, to add to that - how do I make it run on port 80 if that is at all possible?

Dave Mackey
  • 4,306
  • 21
  • 78
  • 136
90abyss
  • 7,037
  • 19
  • 63
  • 94

14 Answers14

289

Something like this worked for me. I am guessing this should work for you.

Run webpack-dev using this

webpack-dev-server --host 0.0.0.0 --port 80

And set this in webpack.config.js

entry: [
    'webpack-dev-server/client?http://0.0.0.0:80',
     config.paths.demo
 ]

Note If you are using hot loading, you will have to do this.

Run webpack-dev using this

webpack-dev-server --host 0.0.0.0 --port 80

And set this in webpack.config.js

entry: [
    'webpack-dev-server/client?http://0.0.0.0:80',
    'webpack/hot/only-dev-server',
     config.paths.demo
 ],

....
plugins:[new webpack.HotModuleReplacementPlugin()]
Bhargav Ponnapalli
  • 9,224
  • 7
  • 36
  • 45
  • 1
    Hi, I'm new to this but would like to use redux for my client script. The problem is that the application is a .net application that serves the content and json endpoints. So to do this during development I need to add `` to the page and install some cross domain allowing plugin? These tools look interesting but seem to assume your server is node as well or am I missing something? – HMR Jun 18 '16 at 12:51
  • 1
    Maybe this will help me: https://webpack.github.io/docs/webpack-dev-server.html#proxy I would think to use proxy for everything except requests for javascript files and some requests made by the webpack server, hotloader and redux timetravel. so `/data:image ...`, `... .js` and `/sockjs-node...` – HMR Jun 18 '16 at 13:10
  • 1
    If you get Invalid Host Header, you may need to set disableHostCheck to true in webpack.serve.config.js. See https://github.com/webpack/webpack-dev-server/issues/882. Depending on your situation, it may be a security hole so be careful. – Brian Deterling Mar 07 '18 at 04:17
  • Btw, be careful when binding 0.0.0.0 in public places where someone else may connect to your device. It may lead to your data being stolen. You may need a firewall for the dev server, like this one: github.com/funbox/webpack-dev-server-firewall – Igor Adamenko Aug 02 '20 at 21:06
150

This is how I did it and it seems to work pretty well.

In you webpack.config.js file add the following:

devServer: {
    inline:true,
    port: 8008
  },

Obviously you can use any port that is not conflicting with another. I mention the conflict issue only because I spent about 4 hrs. fighting an issue only to discover that my services were running on the same port.

bkane56
  • 1,669
  • 1
  • 12
  • 17
  • i take it this is instead of specifying the host and port in the entry section of the config file? – JoeTidee Dec 21 '16 at 04:36
  • 4
    Could you please add a link to the [documentation](https://webpack.js.org/configuration/dev-server/#devserver)? – watery Feb 14 '18 at 08:14
  • 3
    Simply adding inline:true worked for me. BTW the webpack file I edited was 'webpack.dev.conf.js (as of Mar 2018). – Sean O Mar 15 '18 at 18:24
  • The opposite for me: `inline:true` caused an error:`[webpack-cli] Invalid options object. Dev Server has been initialized using an options object that does not match the API schema. - options has an unknown property 'inline'. These properties are valid: ...` Removing the inline attribute made it work fine. – Sam Hokin Jun 15 '22 at 13:05
86

Configure webpack (in webpack.config.js) with:

devServer: {
  // ...
  host: '0.0.0.0',
  port: 80,
  // ...
}
Beder Acosta Borges
  • 5,238
  • 2
  • 28
  • 23
  • 2
    it's not working for for. devServer: { inline: true, host: '0.0.0.0', port: 8088 }, – NK Chaudhary Sep 29 '16 at 12:22
  • Its working for me when I use(webpack.config): devServer: { host:'xx.xx.xx.xxx', port: 8089 }, – Dijo Dec 19 '16 at 21:16
  • This solved a very closely related problem for me while trying to follow the [Modern React with Redux course on Udemy](https://www.udemy.com/react-redux/). I'm running my development environment on Windows in a [Vagrant](https://www.vagrantup.com/) virtual box. The course provides absolutely no guidance about setting up the development environment. – Vince Jan 18 '17 at 00:04
  • Yeah, this addressed an issue I was having running webpack on a Cloud9 instance. Thanks! – Maniaque Mar 12 '17 at 18:53
  • 1
    I was able to change the port number using the above snippet but the host still didn't change. I tried to make the host 0.0.0.0 so that I can access the site from ip-address but it didn't work. Any help in how to achieve this? – Rito Oct 04 '17 at 10:12
  • It's the best answer because I'm using `package.json` file for configuration and I should use a new HOST. –  May 11 '19 at 10:45
39

I am new to JavaScript development and ReactJS. I was unable to find an answer that works for me, until figuring it out by viewing the react-scripts code. Using ReactJS 15.4.1+ using react-scripts you can start with a custom host and/or port by using environment variables:

HOST='0.0.0.0' PORT=8080 npm start

Hopefully this helps newcomers like me.

Anonymous
  • 407
  • 4
  • 2
22

Following worked for me -

1) In Package.json add this:

"scripts": {
    "dev": "webpack-dev-server --progress --colors"
}

2) In webpack.config.js add this under config object that you export:

devServer: {
    host: "GACDTL001SS369k", // Your Computer Name
    port: 8080
}

3) Now on terminal type: npm run dev

4) After #3 compiles and ready just head over to your browser and key in address as http://GACDTL001SS369k:8080/

Your app should hopefully be working now with an external URL which others can access on the same network.

PS: GACDTL001SS369k was my Computer Name so do replace with whatever is yours on your machine.

SkrewEverything
  • 2,393
  • 1
  • 19
  • 50
Sumeet
  • 391
  • 3
  • 4
10

I struggled with some of the other answers. (My setup is: I'm running npm run dev, with webpack 3.12.0, after creating my project using vue init webpack on an Ubuntu 18.04 virtualbox under Windows. I have vagrant configured to forward port 3000 to the host.)

  • Unfortunately putting npm run dev --host 0.0.0.0 --port 3000 didn't work---it still ran on localhost:8080.
  • Furthermore, the file webpack.config.js didn't exist and creating it didn't help either.
  • Then I found the configuration files are now located in build/webpack.dev.conf.js (and build/webpack.base.conf.js and build/webpack.prod.conf.js). However, it didn't look like a good idea to modify these files, because they actually read the HOST and PORT from process.env.

So I searched about how to set process.env variables and achieved success by running the command:

HOST=0.0.0.0 PORT=3000 npm run dev

After doing this, I finally get "Your application is running here: http://0.0.0.0:3000" and I'm finally able to see it by browsing to localhost:3000 from the host machine.

EDIT: Found another way to do it is by editing the dev host and port in config/index.js.

krubo
  • 5,969
  • 4
  • 37
  • 46
6

If you're in a React Application created with 'create-react-app' go to your package.json and change

"start": "react-scripts start",

to ... (unix)

"start": "PORT=80 react-scripts start",

or to ... (win)

"start": "set PORT=3005 && react-scripts start"

R01010010
  • 5,670
  • 11
  • 47
  • 77
  • or edit node_modules/react-scripts/script/start.js and change DEFAULT_PORT and HOST. Just protect file on overwriting when new versions update files on npm update. – Rogelio Triviño Jun 11 '20 at 09:29
4

I feel dirty for telling you this b/c of the security implications of what you're trying to do, but here you go.

npm run dev -- -h 0.0.0.0 -p 80

ToddJCrane
  • 1,587
  • 1
  • 14
  • 22
3

Following worked for me in JSON config file:

"scripts": {
  "start": "webpack-dev-server --host 127.0.0.1 --port 80 ./js/index.js"
},
Roman
  • 19,236
  • 15
  • 93
  • 97
2

For me: changing the listen host worked:

.listen(3000, 'localhost', function (err, result) {
        if (err) {
            console.log(err);
        }
        console.log('Listening at localhost:3000');
    });

was changed to :

.listen(3000, '0.0.0.0', function (err, result) {
        if (err) {
            console.log(err);
        }
        console.log('Listening at localhost:3000');
    });

and the server started listening on 0.0.0.0

Ankush
  • 499
  • 1
  • 7
  • 17
0

I tried the solutions above, but had no luck. I noticed this line in my project's package.json:

 "bin": {
"webpack-dev-server": "bin/webpack-dev-server.js"

},

I looked at bin/webpack-dev-server.js and found this line:

.describe("port", "The port").default("port", 8080)

I changed the port to 3000. A bit of a brute force approach, but it worked for me.

Sparky1
  • 3,335
  • 6
  • 26
  • 29
0

For me, this code worked. Just add it on your package.json file :

"scripts": {
    "dev-server": "encore dev-server",
    "dev": "webpack-dev-server --progress --colors",
    "watch": "encore dev --watch",
    "build": "encore production --progress"
},

And run the script "build" by running npm run build

phrozion
  • 1
  • 2
0

For windows create file runMobile.bat

set PORT=8081
set HOST=192.168.3.20
npm run dev
user956584
  • 5,316
  • 3
  • 40
  • 50
-2

I tried this to easily use another port:

PORT=80 npm run dev
kyrre
  • 327
  • 3
  • 9