Shrikant Dande
Shrikant Dande

Reputation: 233

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 i.e. 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

Upvotes: 0

Views: 1604

Answers (1)

AngelSalazar
AngelSalazar

Reputation: 3113

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 
    })
  ]
  ...
};

Upvotes: 1

Related Questions