我的项目是基于创建-反应-应用程序。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"
}

其他回答

编辑webpack.config.js并添加你想要在其上运行的端口。这就是你要找的东西:

’devServer: 港口:3005。 historyApiFallback: true, ),

and

输出:{ publicPath:“http://localhost: 3005 /”, },

在Windows中可以通过两种方式实现。

在“\node_modules\react-scripts\scripts\start.js”下搜索 “DEFAULT_PORT”并添加所需的端口号。 例如:const DEFAULT_PORT = parseInt(process.env. port = parseInt)端口,10)|| 9999; 在包中。Json,追加下面的行。 "start": "set PORT=9999 && response -scripts start" 然后使用NPM start启动应用程序。它将在9999启动应用程序 端口。

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

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

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

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

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

谢谢

为什么不干脆

PORT=3030 npm run start

这适用于Linux基本操作系统

"start": "PORT=3500 react-scripts start"