create-react-app - 公共(public)JS文件中的yarn build + create-react-app + env变量

标签 create-react-app yarnpkg

我正在使用yarn和env-cmd来构建我的React应用程序并管理我们的环境变量。这是我的 package.json :

"scripts": {
    "start": "react-scripts start",
    "build:local": "env-cmd .env.local react-scripts build",
    "build:uat": "env-cmd .env.uat react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },

对于 /src/ 文件夹下和 /public/index.htm 中的 JS 文件也可以正常工作。

问题: 如何在 public/*.js 文件中使用变量?

我的变量名为REACT_APP_URL。我尝试使用 %REACT_APP_URL%process.env.REACT_APP_URL 但没有成功。

编辑:我想在构建时注入(inject)这些变量。

最佳答案

我在与 package.json 相同的 src 中有一个文件 .env.development 这对我有用

"start": "REACT_APP_ENV=develpoment npm-run-all -p watch-css start-js",

关于create-react-app - 公共(public)JS文件中的yarn build + create-react-app + env变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50588763/

相关文章:

reactjs - 如何仅使用docker而不是host直接创建react-app?

windows - yarn global add 在 Windows 上损坏了吗?

node.js - yarn 包管理器 : install dependencies from private Bitbucket repository

javascript - ReactJS + 环境变量

reactjs - 我应该在 React 生产中使用 registerServiceWorker 吗?

javascript - 如何从 Create React App/build 文件夹制作 npm 包?

javascript - yarn 遇到^(插入符号)时如何工作?

javascript - create-react-app 在 src 目录之外导入限制

reactjs - 在 Electron + Create React App 中使用文件系统访问 API 写入文件失败

javascript - 使用 Yarn react native 构建错误?