How to make the webpack dev server run on port 80 and on 0.0.0.0 to make it publicly accessible?

后端 未结 13 1175
时光取名叫无心
时光取名叫无心 2020-11-30 17:36

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

Wh

相关标签:
13条回答
  • 2020-11-30 17:52

    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"

    0 讨论(0)
  • 2020-11-30 17:53

    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

    0 讨论(0)
  • 2020-11-30 17:53

    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.

    0 讨论(0)
  • 2020-11-30 17:55

    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.

    0 讨论(0)
  • 2020-11-30 17:55

    I tried this to easily use another port:

    PORT=80 npm run dev
    
    0 讨论(0)
  • 2020-11-30 17:56

    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.

    0 讨论(0)
提交回复
热议问题