简体   繁体   English

我应该如何在Reactjs App中的AWS EC2实例上读取环境变量

[英]How should I read environment variables at AWS EC2 instance in Reactjs App

I want to fetch some credentials from environment variables in my Reactjs app, which would be different for different environment ie Dev/staging/prod. 我想从我的Reactjs应用程序的环境变量中获取一些凭证,这对于不同的环境(即Dev / staging / prod)来说是不同的。

I am doing this in webpack.config.js ` 我正在webpack.config.js中执行此操作

function getDotenvFilePath(){
    if(process.env.NODE_ENV === 'local'){
        console.log('u r in local ');
        return './.env.local';
    }else if(process.env.NODE_ENV === 'dev'){
        console.log('u r in development ');
        return './.env.development';
    }else if(process.env.NODE_ENV === 'int'){
        console.log('u r in int ');
        return './.env.int';
    }
}`

different files for different environments which I will trigger from maven like this npm run-script build:int is that a right approach? 我会从maven触发的不同文件(例如npm run-script build:int )用于不同环境,这是正确的方法吗?

Thanx 感谢名单

small example to read a specific .env file based on the NODE_ENV variable 读取基于NODE_ENV变量的特定.env文件的小示例

I am assuming you have dotenv-webpack plugin setup 我假设您已经安装了dotenv-webpack插件

module.exports = {
  ...
  plugins: [
    new Dotenv({
      path: (process.env.NODE_ENV === 'development' ? './.env.development' ? './.env.production'),
      safe: true, 
      systemvars: true, 
      silent: true 
    })
  ]
  ...
};

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

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