How to specify a port to run a create-react-app ba

2019-01-06 11:09发布

My project is based on create-react-app. npm start or yarn start by default will run the application on port 3000 and there is no option of specifying a port in the package.json.

How can I specify a port of my choice in this case? I want to run two of this project simultaneously (for testing), one in port 3005 and other is 3006

标签: reactjs npm
11条回答
劳资没心,怎么记你
2楼-- · 2019-01-06 11:34

Here is another way to accomplish this task.

Create a .env file at your project root and specify port number there. Like:

PORT=3005

查看更多
不美不萌又怎样
3楼-- · 2019-01-06 11:38

Just update a bit in webpack.config.js:

devServer: {
    historyApiFallback: true,
    contentBase: './',
    port: 3000 // <--- Add this line and choose your own port number
  }

then run npm start again

查看更多
Animai°情兽
4楼-- · 2019-01-06 11:43

You can specify a environment variable named PORT to specify the port on which the server will run.

$ export PORT=3005 #Linux
$ $env:PORT=3005 # Windows - Powershell
查看更多
来,给爷笑一个
5楼-- · 2019-01-06 11:48

You could use cross-env to set the port, and it will work on Windows, Linux and Mac.

yarn add -D cross-env

then in package.json the start link could be like this:

"start": "cross-env PORT=3006 react-scripts start",
查看更多
你好瞎i
6楼-- · 2019-01-06 11:49

Changing in my package.json file "start": "export PORT=3001 && react-scripts start" worked for me too and I'm on macOS 10.13.4

查看更多
登录 后发表回答