如何在react js中更改默认端口

jav*_*ing 0 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:
Run Code Online (Sandbox Code Playgroud)

小智 11

要在另一个端口上启动 APP,请运行以下 npm 命令

npm start --port 3002

或在包使用中进行配置

用下面的替换启动命令

"start": "set PORT=3006 && react-scripts start"
Run Code Online (Sandbox Code Playgroud)


小智 10

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