【问题标题】:Set env in windows command line/ package.json在 windows 命令行/ package.json 中设置 env
【发布时间】:2020-12-03 12:50:48
【问题描述】:

windows 上运行react 应用程序时遇到问题。

该应用最初设置为在 macOS 上运行。
以下是错误消息和 package.json 文件。

问题: 如何为 Windows 启动和构建 PORT

package.json

{
  "name": "tradingview-finnhub",
  "private": true,
  "dependencies": {
    "react": "^16.12.0",
    "react-dom": "^16.12.0",
    "react-scripts": "3.4.0",
    "request-promise": "^4.2.5"
  },
  "scripts": {
    "start": "PORT=8080 react-scripts start",
    "build": "PORT=8080 react-scripts build"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

错误信息

'PORT' is not recognized as an internal or external command,
operable program or batch file.
npm ERR! code ELIFECYCLE
npm ERR! tradingview-finnhub@ start: `PORT=8080 react-scripts start`
npm ERR! Exit status 1

【问题讨论】:

  • 这能回答你的问题吗? How can I set NODE_ENV=production on Windows?
  • 只需从这些脚本中删除 PORT=8080 部分,这应该可以工作(至少,如果它是使用 create-react-app 设置的)。一旦它运行,你就可以返回并声明PORT=3000。从任何一种方法开始都应该给我们洞察力......
  • 这很完美

标签: javascript reactjs windows npm


【解决方案1】:

您只需在 PORT=8080 之前添加 set 并在其后添加 &

应该是这样的:

"scripts": {
    "start": "set PORT=8080 & react-scripts start",
    "build": "set PORT=8080 & react-scripts build"
  },

问题是你需要在运行 react-script 之前设置 PORT 的环境变量,我给出的解决方案就是这样做的。有关更多信息,您可以查看:

https://superuser.com/questions/79612/setting-and-getting-windows-environment-variables-from-the-command-prompt

How do I run two commands in one line in Windows CMD?

【讨论】:

  • 工作就像一个魅力,我也能够通过删除 PORT=8080 并运行 react-script-build 来让它工作。谢谢你的信息!
猜你喜欢
  • 2012-05-13
  • 1970-01-01
  • 2016-10-29
  • 2016-01-14
  • 1970-01-01
  • 2020-06-14
  • 2019-08-09
  • 2018-08-02
  • 2010-10-15
相关资源
最近更新 更多