当我运行构建过程时,如何配置我的prod env vars?

问题描述 投票:0回答:2

我正在构建一个React 16.13.0应用程序。 我想为每个环境配置不同的端点,所以我在一个组件srccontainersFormContainer.jsx中进行了设置。

class FormContainer extends Component {
  static DEFAULT_COUNTRY = 484
  static REACT_APP_PROXY = process.env.REACT_APP_PROXY
    ...

我想在本地构建我的项目。 然而在本地我定义了这个变量

localhost:client davea$ echo $REACT_APP_PROXY
http://localhost:9090

当我运行 "npm run-script build "后,我注意到这个编译到我的构建文件中......。

(function(e){return e.json()})).then((function(t){console.log(t),n=t.map((function(e){return e})),e.setState({provinces:n})}))}}]),t}(n.Component);S.DEFAULT_COUNTRY=484,S.REACT_APP_PROXY="http://localhost:9090"

有什么办法可以让React不自动解析envari,而是从生产环境中抓取? 也许我需要调整我的构建脚本? 下面是我的package.json文件中定义的内容......

localhost:client davea$ cat package.json 
{
  "name": "client",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.4.0",
    "@testing-library/user-event": "^7.2.1",
    "bootstrap": "^4.4.1",
    "jquery": "^1.9.1",
    "react": "^16.12.0",
    "react-bootstrap": "^1.0.0-beta.17",
    "react-dom": "^16.12.0",
    "react-native-flash-message": "^0.1.15",
    "react-router-dom": "^5.1.2",
    "react-scripts": "3.3.1",
    "typescript": "^3.8.3"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "proxy": "http://localhost:8000",
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}
reactjs npm environment-variables npm-scripts
2个回答
0
投票

你正在使用react-scripts来构建你的App。你可以在你的.env文件中定义环境变量。

对于节点环境中常见的变量,你可以在.env文件中定义它们。

对于特定的变量 developmentproduction,你可以在 .env.development.env.production 档案

另外,请在变量前加上 REACT_APP

一旦你做到了这一点,你就可以在你的package.json中添加脚本,为特定的 NODE_ENV

"scripts": {
    "start": "react-scripts start",
    "build": "NODE_ENV=development react-scripts build",
    "build:prod": "NODE_ENV=production react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },

然后你就可以在本地建立你的APP进行生产,如

yarn run build:prod

并且它将使用生产环境变量

阅读更多关于它在创建反应应用程序 文件


4
投票

以下步骤可能会有帮助。试试吧。

你也可以参考原始文件获取更多信息。

添加自定义环境变量

第一步

创建 .env 文件在项目的根目录下

制作后的项目结构 .env 文件。

...
- build
- public
- src
    |----- App.js
    |----- index.js
- package.json
- .env
...

里面的 .env 文件:(例如) 前缀 REACT_APP_ 是必须的!

REACT_APP_URL_DEVELOPMENT=http://localhost:8000/api
REACT_APP_URL_PRODUCTION=https://productionDomain.com/api/

第二步。

package.json文件。

Scripts 不需要更改 package.json 文件。

{
  "name": "my-app",
  "version": "0.1.0",
  "private": true,
  "homepage": "http://myWebsite.com",
  "dependencies": {
    ...
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

第三步:

最后,你可以有条件地使用你创建的变量。

例如

class Posts extends Component{
    ...

    componentDidMount(){

      // Finally, you can conditionally use the variables you created.
      const url = process.env.NODE_ENV === "development" ? process.env.REACT_APP_URL_DEVELOPMENT : process.env.REACT_APP_URL_PRODUCTION;

      axios.get(url)
        .then(res => { ... })
        .catch(err => { ... });

    }

    render(){
      return(
        ...
      )
    }
}

Faqs部分:

process.env 是你的环境通过NodeJs提供的一个全局对象。因为我们在浏览器中没有NodeJS。但是你可以访问 NODE_ENV 通过使用 create-react-appwebpack 默认包含,并执行上述步骤。更多细节如下。

有一个内置的环境变量叫做 NODE_ENV. 您可以通过以下方式访问它 process.env.NODE_ENV. 这个变量根据你当前所处的模式而改变。当你运行 npm start它等于 development,当你运行 npm test 它等于 test而当你运行 npm run build 它等于 production. 这个变量很特别,因为它可以用来根据你的模式访问不同的环境配置。

© www.soinside.com 2019 - 2024. All rights reserved.