我的项目是基于创建-反应-应用程序。NPM启动或yarn启动默认将在端口3000上运行应用程序,在package.json中没有指定端口的选项。

在这种情况下,如何指定自己选择的端口?我想同时运行这个项目的两个(用于测试),一个在端口3005,另一个是3006


当前回答

可以在包中指定端口。通过定义端口号:

"scripts": {
"start": "PORT=3006 react-scripts start"}

或 在终端运行脚本时可以指定端口:

PORT=3003 npm start

其他回答

对于windows操作系统,可直接在cmd命令下执行此命令

set PORT=3001 && npm start

为什么不干脆

PORT=3030 npm run start

你可以在package.json中修改你的脚本:

- MacOs:

"scripts": {
     "start": "PORT=9002 react-scripts start",
     "build": "react-scripts build",
     ...     
}

在窗户

  "scripts": {
      "start": "set PORT=9002 && react-scripts start",
      "build": "react-scripts build",
      ...
}

方法1

在项目的根目录下创建.env文件

像这样设置

PORT=3005

方法2

在package.json

set PORT=3006 && react-scripts start

在你的包裹里。在脚本中使用——port 4000或set port =4000,如下例所示:

包中。json (Windows):

"scripts": {
    "start": "set PORT=4000 && react-scripts start"
}

包中。json (Ubuntu):

"scripts": {
    "start": "export PORT=4000 && react-scripts start"
}