【问题标题】:React is not fetching env variablesReact 没有获取环境变量
【发布时间】:2019-07-10 05:53:14
【问题描述】:

尝试在反应中使用环境变量,我得到了一个

未定义

我没有使用 webpack,只是 dotenv。不知道为什么它不工作。

我引用了这个

React env variables with .env

它没有我正在寻找的解决方案。

package.json

{
  "name": "client",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@material-ui/core": "^3.9.1",
    "@material-ui/icons": "^3.0.2",
    "axios": "^0.18.0",
    "history": "^4.7.2",
    "http-proxy-middleware": "^0.19.1",
    "material-ui-icons": "^1.0.0-beta.36",
    "moment": "^2.24.0",
    "react": "^16.7.0",
    "react-dom": "^16.7.0",
    "react-redux": "^6.0.0",
    "react-router-dom": "^4.3.1",
    "react-scripts": "2.1.3",
    "redux": "^4.0.1",
    "redux-thunk": "^2.3.0"
  },
  "scripts": {
    "start": "PORT=8001 react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ],
  "devDependencies": {
    "dotenv": "^6.2.0"
  }
}

Home.js

import React, {Component} from 'react';
// import axios from 'axios';
import Grid from '@material-ui/core/Grid';
import Paper from '@material-ui/core/Paper';
import {withStyles} from '@material-ui/core/styles';
import Chip from '@material-ui/core/Chip';
import {signWithGithub} from '../actions/';
import {connect} from 'react-redux';
import {compose} from 'redux';
import Avatar from '@material-ui/core/Avatar';
import Axios from '../Axios'
import {Redirect, withRouter} from 'react-router-dom';
const styles = theme => ({
    root: {
        flexGrow: 1,
        padding: 20
    },
    paper: {
        padding: theme.spacing.unit * 2,
        textAlign: 'center',
        color: theme.palette.text.secondary
    },

    chip: {
        margin: theme.spacing.unit
    }
});

class Home extends Component {

    constructor(props) {
        super(props);

        this.state = {
            user: ""
        }

    }

    componentWillMount = () => {
        console.log(process.env.BASE_GITHUB_SIGNIN);
    }

.env

BASE_URL=http://localhost:8000
BASE_SIGN_IN=http://localhost:8000/api/users/loginUser
BASE_GITHUB_SIGNIN=http://127.0.0.1:8000/api/users/auth/github

【问题讨论】:

    标签: node.js reactjs


    【解决方案1】:

    如果您使用 create-react-app 创建应用程序,则 env 具有标准格式,例如 here

    只需在每个变量前面加上 REACT_APP,就可以了

    REACT_APP_YOUR_VARIABLE_NAME
    

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题。如果您尚未完成 .env 文件,请在您的主目录中执行此操作。记得启动你的 react 应用程序,这将允许 React 读取 .env 文件。我创建了一个 util 目录并执行了一个函数调用来返回我的 .env 文件中的变量。我从我的 get 请求中调用了函数。

      `export const API_KEY = () => {
      
        return process.env.REACT_APP_API_KEY; // will return API KEY in .env file.
      };
      
      export const API_URL = () => {
        return process.env.REACT_APP_API_URL; // will return API URL in .env file.
      };
      

      ` 完成此操作后,该函数会将 .env 变量返回给您的 API 调用。

      ` axios.get(API_URL(), {
            params: { query: term },
            headers: {
              Authorization: `Client-ID ${API_KEY()}`,
            },
          });`
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-27
        • 2021-01-07
        • 1970-01-01
        • 1970-01-01
        • 2023-02-20
        • 1970-01-01
        • 2019-08-14
        • 1970-01-01
        相关资源
        最近更新 更多