如何在React JS中更改默认端口

时间:2019-06-25 16:24:23

标签: javascript node.js reactjs create-react-app

我能够使用默认端口3000启动react js。但是,当我将端口自定义为4200 "start": "PORT=4200 react-scripts start"(只是在package.json中进行了更改)时,我无法启动react js应用程序

'PORT' is not recognized as an internal or external command,
operable program or batch file.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! todo-app@0.1.0 start: `PORT=4200 react-scripts start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the todo-app@0.1.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional lo
ging output above.

npm ERR! A complete log of this run can be found in:

2 个答案:

答案 0 :(得分:0)

要在其他端口上启动APP,请运行以下npm命令

npm start --port 3002

或配置为使用包

将启动命令替换为

"start": "set PORT=3006 && react-scripts start"

答案 1 :(得分:0)

另一个答案中未提及的另一个选项是在根目录中创建一个.env文件,并将PORT=4200放入其中。它将自动加载到您的环境变量中。

相关问题