简体   繁体   English

ReactJS +环境变量

[英]ReactJS + Environment Variables

Just another React Question. 只是另一个反应问题。 I want to store some URL information globally and divided by environment (dev, production etc...). 我想在全局存储一些URL信息,并按环境(开发,生产等)划分。

Searching on web, the usual method is .env files that are naturally supported by react. 在网上搜索,通常的方法是反应自然支持的.env文件。

I have create a .env and a .env.development so the first will be the production and the second the development file. 我创建了一个.env和一个.env.development,所以第一个是生产,第二个是开发文件。

Inside the file, I put my configuration like: 在文件中,我把我的配置如下:

# .env.development
REACT_APP_TEST=newyork

I tried to print the values with: 我尝试用以下方法打印值:

console.log(process.env);
console.log(process.env.REACT_APP_TEST);   

What I get is: 我得到的是:

Object
  NODE_ENV:"development"
  PUBLIC_URL:""

The second is undefined. 第二个是未定义的。

Where is the problem? 问题出在哪儿?

If you are using create-react-app, you will need to update your env.js to include your new environment variable in the build. 如果您使用的是create-react-app,则需要更新env.js以在构建中包含新的环境变量。

The function getClientEnvironment(publicUrl) sets the environment variables that get injected into the build for your use. 函数getClientEnvironment(publicUrl)设置注入构建的环境变量供您使用。 Here, you can add your custom env variable like so: 在这里,您可以添加自定义env变量,如下所示:

  {
    // Useful for determining whether we’re running in production mode.
    // Most importantly, it switches React into the correct mode.
    NODE_ENV: process.env.NODE_ENV || 'development',
    // Useful for resolving the correct path to static assets in `public`.
    // For example, <img src={process.env.PUBLIC_URL + '/img/logo.png'} />.
    // This should only be used as an escape hatch. Normally you would put
    // images into the `src` and `import` them in code to get their paths.
    PUBLIC_URL: publicUrl,
    CUSTOM: process.env.CUSTOM || 'fallback'
  }

UPDATE: 更新:

If you're not using CRA, you can use the webpack DefinePlugin to do the same thing. 如果您不使用CRA,则可以使用webpack DefinePlugin执行相同的操作。

UPDATE 2: 更新2:

@DimitarChristoff pointed out that you should use the REACT_APP_* format for declaring env variables. @DimitarChristoff指出你应该使用REACT_APP_*格式来声明env变量。 If you prepend your env variable with REACT_APP_ , CRA will automatically pick it up and add it to the Webpack DefinePlugin: 如果您使用REACT_APP_前缀您的env变量,CRA将自动获取它并将其添加到Webpack DefinePlugin:

// Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be
// injected into the application via DefinePlugin in Webpack configuration.
const REACT_APP = /^REACT_APP_/i;

It was more simple than expected. 它比预期的更简单。 The .env files has to be placed in the root of the project NOT in the SRC folder. .env文件必须放在项目的根目录中而不是SRC文件夹中。

That's all! 就这样!

In general Webpack case, you can use webpack's DefinePlugin to define variables to be accessible across app. 在一般的Webpack案例中,您可以使用webpack的DefinePlugin来定义可在app上访问的变量。

In CRA (Create-React-App), you can add the environment variable in env.js file. 在CRA(Create-React-App)中,您可以在env.js文件中添加环境变量。

Using env.js is same as making your own config.js file where you create your config variable with a value either from process.env or use a default. 使用env.js与创建自己的config.js文件相同,在该文件中使用process.env创建配置变量或使用默认值。

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

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