-
Notifications
You must be signed in to change notification settings - Fork 944
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
webpack.DefinePlugin and process.env.DEBUG #467
Comments
I too have this issue. @wontem , did you find a solution to this? |
@aga5tya and @wontem and anyone else who comes along here if (localStorage && process.env.NODE_ENV === 'development') {
localStorage.debug = process.env.DEBUG;
} FYI - my first attempt checking @maintainers - this should be closable and maybe add the snippet to the README for the webpackers out there using the DefinePlugin |
Although it isn't exactly the same issue, I had an issue where if I didn't specify the DEBUG environment variable, webpack was importing all of my environment variables - with the end result being my build environment variables ending up in my final source code. Not good. The quick and easy solution was to just add DEBUG="" to the environment variables. When webpack builds, it finds it, and just includes the empty string instead of all of the environment variables. The issue was in browser.js, in the 'load' method, and after webpack did its thing, the output looked like this: Instead, if you add DEBUG="" as an environment variable, you get this: I do not know if this is due to my specific webpack configuration or what, but I thought I'd mention it incase anyone else has a similar issue. |
@wontem you have to escape values that you want assigned to actual variables. Otherwise, Webpack is going to REPLACE the variables with literal values. Should look like this, afaik: plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production'),
DEBUG: JSON.stringify(true)
}
})
], |
@SPGWhistler Great find and good suggestion -- unfortunately it didn't work for me. I don't know, may be because I'm running in create-react-app. This is definitely a show-stopper! |
@wontem did @thebigredgeek's comment answer your question? |
@Qix- yes, the issue can be closed |
This doesn't work when using {
'process.env.DEBUG': JSON.stringify( true ),
'process.env.NODE_ENV': JSON.stringify( 'development' )
} You still get the invalid |
Could you set a variable to reference function save(namespaces) {
const env = process.env;
if (null == namespaces) {
// If you set a process.env field to null or undefined, it gets cast to the
// string 'null' or 'undefined'. Just delete instead.
delete env.DEBUG;
} else {
env.DEBUG = namespaces;
}
} I believe this would prevent DefinePlugin with messing with it. |
Sorry about that. |
I know this is painful for some users, but It is not |
Was stuck on this problem for a long time, but found a hacky way to configure webpack.DefinePlugin in order to allow for the existence of a runtime process.env object. First, add the following line to your webpack.DefinePlugin configuration
Feel free to name it something better than Then in your app's index.js, ensure that a global
The line |
Also @qix , concerning your claim about process.env having a "very clear contract" , can you link me to some supporting resources? Apart from this webpack.DefinePlugin issue, I'm not sure that runtime code should be mutating environment variables. Is there a way we could refactor this code to avoid process.env.DEBUG assignment? Could that state be stored elsewhere? |
I agree that setting |
My webpack config:
So webpack changes
process.env.DEBUG = namespaces;
to
true = namespaces;
which leading to error
ReferenceError: Invalid left-hand side in assignment
The text was updated successfully, but these errors were encountered: