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

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


当前回答

在你的包裹里。在脚本中使用——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"
}

其他回答

在我的包里换衣服。“export PORT=3001 && react-scripts start”也适用于我,我在macOS 10.13.4上

试试这个:

npm start port=30022

在你的包裹里。在脚本中使用——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"
}

您需要更新您的软件包。json来指定不同的PORT

在脚本部分,替换start命令如下:—

确保所提到的PORT可以自由监听

"start": "export PORT=3001;react-scripts开始”

您的应用程序将从http://localhost:3001开始

谢谢

为什么不干脆

PORT=3030 npm run start