You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Simple question, but I'm a little lost with the way webpack.DefinePlugin interacts with the application.
For my http calls, I'd like to have a variable that is dynamic based on the process.env.ENV. What's the best way to define a variable in both my webpack.config.js and webpack.prod.config.js files? And what's the best way to make them global variables in the application?
For development I use dotenv to set the environment variable.
For production just set environment variable as usual.
Reacted by June Domingo, Fabio Yamada, Jeff Shamley, Elliot Larson, Mike Staub, Robby Emmert, Vishal Sharma, Mehul Patel, AleksOrl, Caio Gondim and 112 more
Reacted by miguel, Guillim, Frank Valdez and Elle
Reacted by Rockwell Schrock, Christian Rocha, David Baker, Paweł Iżycki, Ali UYGUR, d-g-h, Illia Olenchenko, miguel, CARLOS H. OLIVEIRA, Tan Jun Xiang and 6 more
Reacted by Josh Biddick, miguel, pacificsoftdevld, CARLOS H. OLIVEIRA, Frank Valdez, Spencer Smith, Elle, Khairul Islam, Mohammad Zolfaghari and Emil Cieslar
Reacted by Sasha Goldenson, Petar Bojinov, Abe Rubenstein, David Sánchez, mohsenjavidpanah, Hamid Behnam, Josh Morel, Ali UYGUR, mzvast, Jason Caylor and 3 more
Reacted by Jasper Poppe, Sara Lara, Andrew Faulkner, Marios Fakiolas, Mohamad Nimer, Sasha Goldenson, sataylor7, Hamid Behnam, ewinchell07, Danny Althoff and 2 more
@marek1 I noticed the same, there is a strange behaviour: When I de console.log(process) there is an empty env object .
This strange behavior becomes clear when you look at the packed webpack file. Basically process does not get added to the global scope what so ever. Webpack does a search and replace for anything that is process.env and fills it in with that object specifically everywhere. So essentially it is a new instance of that config object every time. I think this was to not pollute the global space, but for minify purposes seems like a waste.
Is there a option to make it part of the global namespace?
@HendrikRoth please don't ever do this, as you've just exposed the environment of the process that you built with to your bundle... And subsequently the internet. Many sensitive values are potentially contained therein, unless DefinePlugin is sanitizing them somehow... Although I can't imagine how.
The plugin new webpack.DefinePlugin({ 'process.env.NODE_ENV': process.env.NODE_ENV }) will result in code like this when process.env.NODE_ENV is the string 'production':
The config new webpack.DefinePlugin({ 'process.env': process.env }), while simpler, will instruct webpack to replace process.env with an object matching the value provided, so that subsequent keys can be retrieved, e.g.
// some/project/file.jsif(({/* all of process.env as key: value */}.NODE_ENV==='production'){doSomethingForProduction();}
The suggestion you made @gezichenshan, would be much safer, but why not just put those definitions in the DefinePlugin config instead of an external file? If there were lots of values that you needed to pick out of process.env, then maybe a module that did just that would make sense:
const_=require('lodash');constKEYS=['NODE_ENV','BASE_URL','SOMETHING_ELSE',// . . . so many others];module.exports=_.chain(KEYS).map(key=>`process.env.${key}`).map(path=>[path,_.get(process.env,path)]).fromPairs().value();
But likely that's not practical, either, since that would require a large number of environment variables passed to the startup script. At that point, another external configuration file would be better. Probably one that switches values dependent on... process.env.NODE_ENV...!
I probably should've caught this right away, but I wasn't wrapping the values in JSON.strinify() and was throwing console errors that listed the key, but said it wasn't defined. Which makes sense, but took me a few minutes to figure out why. Hope this helps others in the future.
Originally I was using the below new webpack.DefinePlugin({ 'process.env': { 'WP_ENV': process.env.WP_ENV, 'WP_DEBUG': process.env.WP_DEBUG, } }),
Then updated it to this to make it work properly new webpack.DefinePlugin({ 'process.env': { 'WP_ENV': JSON.stringify(process.env.WP_ENV), 'WP_DEBUG': JSON.stringify(process.env.WP_DEBUG), } }),
Activity
rbao commentedon Mar 5, 2016
Not sure if this is the best way, but here is how I do it in both
webpack.config.jsandwebpack.prod.config.js:Then you can just use it in the code
For development I use dotenv to set the environment variable.
For production just set environment variable as usual.
louisscruz commentedon Mar 6, 2016
Perfect. Thanks!
PatrickJS commentedon Mar 7, 2016
@rbao is correct. @rbao can you make a wiki page to talk briefly about this https://github.com/AngularClass/angular2-webpack-starter/wiki
jshamley commentedon Aug 4, 2016
Don't forget to restart Webpack after adding the environment variables or the system won't recognize them.
marek1 commentedon Sep 21, 2016
I did all those things mentioned above, but
when running npm start i get a process object that contains an EMPTY env object
herr-bubu commentedon Oct 28, 2016
I do it like this:
and then in JS:
Remco75 commentedon Nov 20, 2016
@marek1 I noticed the same, there is a strange behaviour: When I de
console.log(process)there is an empty env object .When I do
console.log(process.env)it is filled with the proper stuff. I think it has to do with the string 'process.env' we set in the DefinePluginnmccready commentedon Nov 21, 2016
@Remco75
This strange behavior becomes clear when you look at the packed webpack file. Basically process does not get added to the global scope what so ever. Webpack does a search and replace for anything that is process.env and fills it in with that object specifically everywhere. So essentially it is a new instance of that config object every time. I think this was to not pollute the global space, but for minify purposes seems like a waste.
Is there a option to make it part of the global namespace?
Example:
client code (source mapped):
Real Packed Code:
client code (packed):
It is not what you assumed it was
nmccready commentedon Nov 21, 2016
Also this is talked about in some detail here webpack/webpack#868
To get this working a little nicer for consuming your config objects I wrote this:
jianghai commentedon Nov 29, 2016
So, how could I do if
__LANG__is not defined byDefinePlugin? Like thisHendrikRoth commentedon May 31, 2017
Why not just this?
al-the-x commentedon Dec 9, 2017
@HendrikRoth please don't ever do this, as you've just exposed the environment of the process that you built with to your bundle... And subsequently the internet. Many sensitive values are potentially contained therein, unless
DefinePluginis sanitizing them somehow... Although I can't imagine how.gezichenshan commentedon Feb 11, 2018
@al-the-x so it means the webpack.DefinePlugin rewrites the process.env as a global variable in a webpack project?
../config/dev.env:
so can we change the process.env in
'process.env': require('../config/dev.env')to another name like 'globalEnv'al-the-x commentedon Mar 2, 2018
In the example from above to which I objected, consider code like this:
The plugin
new webpack.DefinePlugin({ 'process.env.NODE_ENV': process.env.NODE_ENV })will result in code like this whenprocess.env.NODE_ENVis the string'production':The config
new webpack.DefinePlugin({ 'process.env': process.env }), while simpler, will instructwebpackto replaceprocess.envwith an object matching the value provided, so that subsequent keys can be retrieved, e.g.The suggestion you made @gezichenshan, would be much safer, but why not just put those definitions in the
DefinePluginconfig instead of an external file? If there were lots of values that you needed to pick out ofprocess.env, then maybe a module that did just that would make sense:But likely that's not practical, either, since that would require a large number of environment variables passed to the startup script. At that point, another external configuration file would be better. Probably one that switches values dependent on...
process.env.NODE_ENV...!skwid138 commentedon Jul 6, 2018
JSON not JS
I probably should've caught this right away, but I wasn't wrapping the values in JSON.strinify() and was throwing console errors that listed the key, but said it wasn't defined. Which makes sense, but took me a few minutes to figure out why. Hope this helps others in the future.
Originally I was using the below
new webpack.DefinePlugin({ 'process.env': { 'WP_ENV': process.env.WP_ENV, 'WP_DEBUG': process.env.WP_DEBUG, } }),Then updated it to this to make it work properly
new webpack.DefinePlugin({ 'process.env': { 'WP_ENV': JSON.stringify(process.env.WP_ENV), 'WP_DEBUG': JSON.stringify(process.env.WP_DEBUG), } }),