Is that an upcoming feature or why isn’t the variable set when I try to access it in the webpack config with process.env.NODE_ENV ? I use webpack ^2.1
Furthermore I recognized that when using -p option the UglifyJsPlugin is applied? Don’t see that in the documentation anywhere.
NODE_ENV
is set in the compiled code, not in the webpack.config.js file. You should not use enviroment variables in your configuration. Pass options via--env.option abc
and export a function from the webpack.config.js.Why should you not use env-variables in the config?
I have a webpack config, which changes according to NODE_ENV. It’s awesome because it’s in one file and
webpack-dev-server
NODE_ENV='production' webpack -p
I noticed too that for some reason the output of “webpack -p” is different than just running
webpack
with the Uglify plugin applied.Personally, I am not quite getting what you mean by
-env.option abcd
but I tried this and it doesn’t seem to help with anything. I do believe that doing this automatically based on the-p
flag would be reasonable. For the loads of other people that have also run into this behavior wondering “wtf!?” below is dead simple example of what I am currently using to deal with the issue:_package.json_
_webpack.config.js_
_src/main.js_
_src/dev.js_
src/prod.js
The key here is really only the
webpack.config.js
I only included the rest for it to be a complete example of it’s usage. Below we can see the results:This is much mode in-line with what I believe users expect when using the
-p
flag. It would be REALLY nice if this was the default behavior when using the flag. I would be more than willing to submit a PR if this is deemed acceptable default behavior. I know I ran into this issue and ran around in circles working around it and while I was I know I looked up quite a few “solutions” (some blogged about) meaning there are quite a few people with this specific problem.Agree, it’s really confusing that is says this in the changelog:
and then
process.env.NODE_ENV
is undefined in the webpack config.I usually call webpack setting
NODE_ENV
from the command line (NODE_ENV=production webpack -p
), and then add or remove some plugins based on that.EDIT: here is an example of what I’m doing, webpack-dashboard has an issue and so it needs to be included only in development
I agree this is an issue. I spent several hour on it before I found out what is causing my problems 🙁
There are many articles and repositories which depend on having NODE_ENV set in config file already.
What’s the extra optimisation that
-p
performs?The key part for me was to understand that environment variables are not available in the webpack build process unless specified. That’s why we need the following line from @fab1an‘s great example :
….which can be stated more clearly using the
EnvironmentPlugin
, which directly passes environment variables to the build:Currently I’m handling this issue with
webpack -p --env production
.And then in
webpack.config.js
:I do agree the
-p
and--env production
is a bit repetitive.It seems that most people here thinks that
webpack -p
is mandatory to do a production build, but am I the only one thinking it should not be this way?Not all are running webpack for production via the CLI, but programmatically via node. Shouldnt
webpack -p
be the same as running webpack withNODE_ENV=production
?-p
automatically addsas plugins. It is not mandatory, it just enables those plugins for you for convenience.
Because of the DefinePlugin,
process.env.NODE_ENV
will be replaced in your source files, just like if you had used DefinePlugin in your config by hand. It will not set the node environment variableprocess.env.NODE_ENV
in your webpack config, as the webpack config is run directly by node.Basically, what you want to do is
NODE_ENV=production webpack -p
all the time. https://github.com/jacobmischka/coyote-grill/blob/master/package.json#L10In order, to determine if I am in development or production mode I use the following code in my webpack.config.js:
then I can use it like:
I hope it helps for someone looking for an alternative.
I do the following:
and then in the webpack config:
Now I just do🎉
npm run build
and everything is minified!The second argument of your config function contains webpack calling options. This is undocumented feature, but might be useful, anyway.
Wanting
-p
to setNODE_ENV
to “production” is a bizarre request.-p
indicates a production build, whileNODE_ENV === "production"
indicates a [runtime] production environment. I’ve never understood why devs tie these things together. I might want to test my production build in a local dev environment, in which case I now have to spoofNODE_ENV
at build time. Unless you’re actually using Webpack in a runtime production environment, I’m not sure why you’d want your config to rely onNODE_ENV
at all. It seems like it would be mixing two entirely unrelated concerns.I finally settled on the EnvironmentConfig’s defaulting behavior as in:
This just sets the default NODE_ENV value if not already defined.
If you also set it on the command line, as in
NODE_ENV=production webpack ...
, the command line wins. To me, that is intuitive and desirable.Yes it’s confusing to write
NODE_ENV=production webpack -p ...
as many have pointed out, but when the light bulb finally went on I realized this is really two different concepts.The solution is simple: rename
-p
to-o
for optimize, which is exactly what this does. That way we can disassociate this command with the concept of production/environment variables. It should never have been called-p
anyway.Why not using:
And inside
webpack.config.js
:Please clarify your documentation then. (Add an additional mark, stating that it does not set it for the config itself)
https://webpack.js.org/guides/production/#cli-alternatives
states that -p sets both flags. But in webpack 3.9.1 it still only sets this variable for packages run by webpack and not the webpack config itself.
Why use it?
See my repo: https://github.com/SamanthaAdrichem/webpack-3.9.1-splitted-config-angular/
i’m using it to load separate development and production configs -p now feels like a useless argument
I’ve made a webpack plugin,
node-env-webpack-plugin
, that works around this by settingprocess.env.NODE_ENV
toproduction
in the Node.js process when you runwebpack -p
.@mittalyashu It’s in
webpack --help
:After 16 months the hackaton ended. They had finally found a way to set the environment in webpack.
This is now fixed in webpack 4. Setting mode: production or development also sets the proper NODE_ENV
I sometimes use
NODE_ENV
outside the context of bundling. Is there a command-line flag or configuration setting to prevent--mode
from changingNODE_ENV
?Update: yes there is.🎉 😄
Update 2: this is still bad, do this:
This should make it so webpack leaves
process.env.NODE
in your bundled code rather than resolving it to whateverNODE_ENV
is at compile-time, which is what you’d want in the dumb example above.