javascript 导入环境变量反应前端
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49108136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
importing env variable react front end
提问by peter flanagan
I have bootstrapped an app using create-react-app. I have a token that I don't wish to push to GitHub.
我已经使用create-react-app. 我有一个不想推送到 GitHub 的令牌。
I have ran yarn add dontenvand then tried to import the env variable to my App.jsfile.
我已经运行yarn add dontenv,然后尝试将 env 变量导入到我的App.js文件中。
My code looks like this
我的代码看起来像这样
.env
.env
TOKEN=**************
Then my app.jsfile looks like this:
然后我的app.js文件看起来像这样:
app.js
应用程序.js
import React from 'react';
import ReactDOM from 'react-dom';
require('dotenv').config();
const App = props => {
console.log(process.env.token);
return <p>Test</p>
}
process.env.tokenis undefined. Can anyone advise how to use token in the front end or how I should do this using a bootstrapped create-react-app?
process.env.token是undefined。任何人都可以建议如何在前端使用令牌,或者我应该如何使用引导式 create-react-app 来做到这一点?
回答by Joe Clay
You don't need dotenv(and I doubt that library will work at runtime in a client-side application anyway).
您不需要dotenv(而且我怀疑该库无论如何都不会在客户端应用程序中在运行时工作)。
create-react-appactually provides this functionality out of the box, assuming you're using [email protected]or higher.
create-react-app假设您正在使用[email protected]或更高版本,实际上提供了开箱即用的此功能。
The steps are as follows:
步骤如下:
- Create a
.envfile in the root of your project. - Add a variable, starting with the prefix
REACT_APP_. - Access it via
process.env.
.env在项目的根目录中创建一个文件。- 添加一个变量,以前缀 开头
REACT_APP_。 - 通过访问它
process.env。
That second bit is the important part - to avoid you exposing variables by accident, create-react-appforces you to use a prefix as a way of saying "yes, I know what I'm doing".
第二部分是重要的部分 - 为避免您意外暴露变量,create-react-app强制您使用前缀作为说“是的,我知道我在做什么”的方式。
If you're not intending to push the file to source control (which you shouldn't be, if it's got secret keys in!), then using an .env.localfile is more idiomatic - that requires [email protected]or higher, though.
如果您不打算将文件推送到源代码管理(您不应该这样做,如果它有秘密密钥!),那么使用.env.local文件更惯用 - 但这需要[email protected]或更高。
回答by Cyrus Zei
for anybody that does not get this to work, try this
对于任何不能让它工作的人,试试这个
if you want git to ignore it you can create a .env.localfile and git will ignore it. Look at your .gitignore file and you will find the `.env.local``
如果你想让 git 忽略它,你可以创建一个.env.local文件,git 会忽略它。查看你的 .gitignore 文件,你会发现 `.env.local`
- create
.env.localfile in your root folder - Open your
.env.local - IMPORTANT, all your environment variables MUST start with
REACT_APP_ - Create an environment variables
REACT_APP_BASE_URL - Restart your application (kill it)
- To access environment variables you write
process.env.REACT_APP_BASE_URL
.env.local在您的根文件夹中创建文件- 打开你的
.env.local - 重要的是,您所有的环境变量都必须以
REACT_APP_ - 创建环境变量
REACT_APP_BASE_URL - 重新启动您的应用程序(杀死它)
- 访问您编写的环境变量
process.env.REACT_APP_BASE_URL

