【问题标题】:How to pass argument in command line with npm and read it in react javascript code如何使用 npm 在命令行中传递参数并在反应 javascript 代码中读取它
【发布时间】:2021-08-24 14:58:10
【问题描述】:

我使用npm start 运行react 应用程序,它在package.json 中定义为"start": "react-scripts start -o"。在我的js代码中

const backendUrl = 'hardCodedUrl';

我希望将此属性作为 CLI 参数传递。喜欢:

npm start url=newUrl

并在运行时在 javascript 代码中读取此内容,以便能够设置 backendUrl。我该怎么做?

【问题讨论】:

标签: javascript reactjs npm command-line-interface command-line-arguments


【解决方案1】:

所以我访问了 Andy 发送的链接,我是这样做的: 因为我在 Windows 上并使用带有 PowerShell 的终端,所以运行:

$env:REACT_APP_TT_BACKEND_URL = "Thank you Andy"
npm start

在代码中

console.log('REACT_APP_TT_BACKEND_URL', process.env.REACT_APP_TT_BACKEND_URL);

【讨论】:

    猜你喜欢
    • 2021-03-04
    • 2023-03-16
    • 2021-06-01
    • 2020-11-29
    • 2016-02-22
    • 2017-02-28
    • 2019-10-27
    • 2018-03-17
    • 1970-01-01
    相关资源
    最近更新 更多