3

I have a reactjs project made using create-react-app

In nodejs how to run npm start for 0.0.0.0:3000

I tried

npm start --host 0.0.0.0:3000

But i am not able to connect from any ip

package.json

{
  "name": "frontend",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
   ........
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@babel/core": "^7.9.0"
  }
}
Santhosh
  • 9,965
  • 20
  • 103
  • 243
  • Can you share the script you're running? Usually npm start points at something like `node index.js` – steadweb Apr 28 '21 at 16:42
  • its a reactjs project – Santhosh Apr 28 '21 at 16:44
  • If it's a `create-react-app` project, npm start should be enough. You usually get an output of the IP(s) it's listening on. Can you share that? – steadweb Apr 28 '21 at 16:46
  • 1
    what about HOST=0.0.0.0 npm start – Santhosh Apr 28 '21 at 16:52
  • I need to see some code to see what it's doing. HOST would be an env var, that I'd assume your project picks up. – steadweb Apr 28 '21 at 16:54
  • Possible duplicate: [How to specify a port to run a create-react-app based project?](https://stackoverflow.com/questions/40714583/how-to-specify-a-port-to-run-a-create-react-app-based-project). – 0stone0 Apr 28 '21 at 16:56
  • @Santhosh Please see marked duplicate. You'll need something like `"start": "export PORT=3000 react-scripts start"` – 0stone0 Apr 28 '21 at 17:02
  • what about the ip, i am not able to access from external i – Santhosh Apr 28 '21 at 17:03
  • 1
    I am looking for more command line. so `HOST=0.0.0.0 PORT=3000 npm start` - will it work – Santhosh Apr 28 '21 at 17:05
  • 1
    `HOST=123.456.789`. C'mon, Just google `create-react-app` `react-scripts ` change port/host. [How to make the webpack dev server run on port 80 and on 0.0.0.0 to make it publicly accessible?](https://stackoverflow.com/questions/33272967/how-to-make-the-webpack-dev-server-run-on-port-80-and-on-0-0-0-0-to-make-it-publ) – 0stone0 Apr 28 '21 at 17:06
  • Anyway, change the port to an IP wouldn't make it a local dev-server. So consider checking thinks like firewalls. – 0stone0 Apr 28 '21 at 17:06
  • Sorry , i found that its the firewall. `npm start` is itself sufficient. it allows the outside ips to connect also – Santhosh Apr 28 '21 at 17:14

1 Answers1

-1

You can't just connect using CLI flags. You need to have files for example, to connect to port 3000 you'll need something like this:

const http = require('http')

const server = http.createServer((req, res) => {
    if (req.url === '/') {
        res.write('<h1>Hello, Node.js!</h1>');
    }
    res.end();
});

server.listen(3000)

this will create a web server listening to port 3000

alec wilson
  • 176
  • 1
  • 3
  • 13