【问题标题】:How to change the default port in react js [duplicate]如何更改反应js中的默认端口[重复]
【发布时间】:2019-11-07 13:06:07
【问题描述】:

我可以使用默认端口 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:

【问题讨论】:

  • 我猜您使用的是 Windows - 该语法是如何在 *nix 中设置环境变量。

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


【解决方案1】:

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

npm start --port 3002

或在包中配置使用

将启动命令替换为下面

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

【讨论】:

  • @Manpreet 它的工作!谢谢。
  • @javalearning 。欢迎
  • 它对我有用
  • 谢谢,它也特别适合我&&
【解决方案2】:

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

【讨论】:

  • 它对我有用...
猜你喜欢
  • 2023-03-30
  • 1970-01-01
  • 2015-05-24
  • 2016-11-02
  • 2011-10-19
  • 1970-01-01
  • 2019-09-08
  • 2019-12-25
  • 1970-01-01
相关资源
最近更新 更多