简体   繁体   English

无法在 create-react-app 生成的应用程序中读取环境变量

[英]can't read environment variables in create-react-app generated app

I used create-react-app to generate a node app.我使用 create-react-app 来生成一个节点应用程序。 I changed the app.js file to look like this:我将 app.js 文件更改为如下所示:

import React from 'react';
import logo from './logo.svg';
import './App.css';
let a = process.env.SOMETHING;
console.log(a)
function App() {
    console.log(process.env)
  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.js</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          { process.env.SOMETHING }
        </a>
          </header>
          <div>The value of something: ${a}</div>
    </div>
  );
}

I would expect the a variable to reflect what I set in the environment.我希望a变量能够反映我在环境中设置的内容。 When I look at the console after starting the app, it is undefined, and all I see is the dollar sign where the variable should be inserted.当我在启动应用程序后查看控制台时,它是未定义的,我看到的只是应该插入变量的美元符号。

I've tried setting the variable in two ways.我尝试以两种方式设置变量。 Firstly, I created a bash file called setup.bash and gave it executable permission.首先,我创建了一个名为setup.bash的 bash 文件并授予它可执行权限。 It looks like this:它看起来像这样:

setup.bash设置.bash

export SOMETHING="boom"

Then I changed package.json to this:然后我将package.json更改为:

{
  "name": "server",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.10.2",
    "react-dom": "^16.10.2",
    "react-scripts": "3.2.0"
  },
  "scripts": {
    "start": "./setup.bash && 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"
    ]
  }
}

When this didn't work, I tried starting the app like this:当这不起作用时,我尝试像这样启动应用程序:

SOMETHING=BOOM npm start

... and the environment variable remained undefined. ...并且环境变量仍未定义。

Can anyone see what the problem might be?谁能看到问题可能是什么?

Create .env.development text file.创建.env.development文本文件。 Then you can list variables there, but all of them must start with REACT_APP_ prefix.然后你可以在那里列出变量,但它们都必须REACT_APP_前缀开头。

For example: REACT_APP_API_URL=https://api.url例如: REACT_APP_API_URL=https://api.url

You can also run the npm start command with variables declared before it:您还可以运行npm start命令,并在其之前声明变量:

REACT_APP_API_URL="https://api.url" npm start , but again, remember about the prefix. REACT_APP_API_URL="https://api.url" npm start ,但同样,请记住前缀。

Read more about environment variable for create-react-app here . 在此处阅读有关 create-react-app 环境变量的更多信息。

It did not work for me after I set REACT_APP_API_URL=https://api.url in .env在 .env 中设置REACT_APP_API_URL=https://api.url后,它对我.env

Then I stop react-app, run npm start and it works fine for me.然后我停止 react-app,运行npm start ,它对我来说很好。

Don't forget create .env in the root directory不要忘记在根目录中创建.env

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM