Get environment variable defined in Linux bash to webpack

Viewed 1935

I have checked out tons of SO questions about "environment variables in webpack" using e.g. the DefinePlugin:

new webpack.DefinePlugin({'ENV': JSON.stringify('staging')})

but I cannot for the life of me find a way to inject an environment variable defined in the linux bash shell, instead of using the hard coded staging string

In my production and staging environements I have variables such as $ENV and $API_KEY defined, and I want to use their values in my webpack / ReactJs code.

Edit

I notice, if I run the a webpack command from cli:

 $ ENVIRONMENT=staging
 $ node_modules/.bin/webpack -p

And in my webpack.config.js file defines

 new webpack.DefinePlugin({'ENV': JSON.stringify(process.env.ENVIRONMENT)})

This does not work (ENV is undefined in my JS code),

However, if I run it on the same line, it seems to work - ENVIRONMENT seems to be available in the webpack.config.js file:

$ ENVIRONMENT=staging node_modules/.bin/webpack -p

So I would really like to make this work without having to define the ENVIRONMENT variable on the same line as the webpack command.

2 Answers
Related