简体   繁体   中英

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.

I am doing this in 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?

Thanx

small example to read a specific .env file based on the NODE_ENV variable

I am assuming you have dotenv-webpack plugin setup

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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