【问题标题】:Port doesn't recognized while running multiple app of react on windows在 Windows 上运行多个反应应用程序时无法识别端口
【发布时间】:2021-02-06 09:03:29
【问题描述】:

我有多个反应应用程序,例如一个父应用程序,在父应用程序中,我还有两个应用程序在不同的端口上运行,父应用程序在 3000 端口上运行,第一个孩子在 3005 端口上运行,第二个孩子在 4000 端口上运行.我在下面给出的脚本部分的 package.json 文件中做了一些配置

父应用有

 "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "odmit": "npm start odmit_view",
    "moderator": "npm start --prefix moderator",
    "monitor": "npm start --prefix monitor",
    "dev": "concurrently \"npm run odmit\" \"npm run moderator\" \"npm run monitor\""
 },

第一个孩子有 package.json 是

"scripts": {
   "start": "PORT=4000 react-scripts start",
   "build": "react-scripts build",
   "test": "react-scripts test",
   "eject": "react-scripts eject"
 },

第二个孩子有 package.json

"scripts": {
   "start": "PORT=3005 react-scripts start",
   "build": "react-scripts build",
   "test": "react-scripts test",
   "eject": "react-scripts eject"
},

当我使用命令npm run dev 在 ubuntu 上运行它时,这会很好。当我在 Windows 中运行相同的命令时,它会给我一个无法识别此端口的波纹管错误。

'PORT' is not recognized as an internal or external command,
 [2] operable program or batch file.
[2] npm ERR! code ELIFECYCLE
[2] npm ERR! errno 1
[2] npm ERR! monitor@0.1.0 start: `PORT=4000 react-scripts start`
[2] npm ERR! Exit status 1
[2] npm ERR!
[2] npm ERR! Failed at the monitor@0.1.0 start script.
[2] npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
[1]
[1] > moderator@0.1.0 start D:\odmit_view\moderator

这是什么问题,谁能帮帮我。

【问题讨论】:

    标签: reactjs npm environment-variables create-react-app


    【解决方案1】:

    在 Windows 上,您需要先设置环境变量:

    "scripts": {
        "start": "set PORT=4000 && react-scripts start"
    }
    

    或者,您也可以使用cross-env 在不同的环境(Windows 和 Ubuntu)上工作,如下所示:

    "scripts": {
        "start": "cross-env PORT=4000 react-scripts start"
    }
    

    【讨论】:

    • 我试过这个窗口工作正常,但现在适用于 ubuntu 我也需要在 ubuntu 中定义一个端口吗?
    • 我们如何在 ubuntu 上做到这一点
    • 如需在 unix & windows 中使用相同的配置,请参考使用cross-env 包的替代方案。第二个例子应该工作
    猜你喜欢
    • 2021-06-10
    • 1970-01-01
    • 2015-03-08
    • 1970-01-01
    • 2020-10-27
    • 2015-11-09
    • 2012-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多