What is the difference between webpack --env.production and --mode="production"

nakhodkin

Correct me if I'm wrong but as far as I understand from the documentation,

--env option is used ONLY for being able to get access to it within webpack.config.js if it exports a function e.g.

module.exports = function(env, options) {
  console.log(env); // "production"
}

and let's say that I have my entry point index.js with the following code:

console.log(process.env.NODE_ENV); // undefined ???

I wonder if
process.env.NODE_ENV won't be assigned to any value regardless I pass --env.production or --env.development

I wonder if
webpack will enable any sort of optimizations automatically depending on a value for --env


--mode option is used to enable some bunch of optimizations right away and it will set process.env.NODE_ENV to be accessible inside my source files, e.g.

console.log(process.env.NODE_ENV); // "production" OR "development", etc ???

I wonder if
process.env.NODE_ENV will be assigned to any value accessing it from within webpack.config.js

I wonder if
Let's say that I run webpack with the following command $ webpack --mode="development"

and I have the following contents of webpack.config.js:

module.exports = {
  devtool: 'source-map'
};

so, will the devtool option eventually be set to eval(if I weren't redefining devtool in my webpack.config.js or the value will be source-map, so it will be rewritten with those from my webpack.config.js file?

Konrad

I asked a similar question here: Webpack environment variables confusion

First of all: both options have nothing to do with process.env.NODE_ENV. Yeah, it's confusing especially because the docs mention NODE_ENV many times.

webpack's environment variables are different from the environment variables of operating system shells like bash and CMD.exe

  • --env command-line option basically allows you to change the value of env.{some property} so if you just pass --env.production env.NODE_ENV will be undefined and env.production will be set to true. You would need to set it separately with --env.NODE_ENV=yourvalue. Note this has nothing to do with process.env. env is just the object passed as parameter to your function exported from webpack.config.js.

  • --mode command-line option was introduced in webpack v4 and you can use it to set process.env.NODE_ENV on DefinePlugin only to one of 3 values - development, production or none. It looks like it was made exclusively for DefinePlugin. If you try to console.log(process.env.NODE_ENV); inside your webpack config it will be undefined. https://github.com/webpack/webpack/issues/7074

If you want to read those options you need to export a function instead of an object from your webpack.config.js.

// webpack --mode=production --env.foo=bar --env.NODE_ENV=production
var config = {
  entry: './app.js'
  //...
};

console.log(process.env.NODE_ENV); // undefined!! unless you really set it in OS or with cross-env

module.exports = (env, argv) => {

  console.log(argv.mode); // will print "production"
  console.log(env.foo); // will print "bar"

  return config;
};

cross-env

People also use cross-env to set process.env.NODE_ENV and they don't use webpack's --env or --mode at all.

The only reason for using cross-env would be if you had multiple configs in your project like postcss.config.js and webpack.config.js and you wanted to set your environment just once, use process.env.NODE_ENV everywhere and be done with it. It just won't work with DefinePlugin out of the box.

You could also do this if you don't want to use cross-env:

module.exports = (env, argv) => {
  process.env.NODE_ENV = argv.mode;

  return config;
};

or set mode based on process.env.NODE_ENV:

var config = {
  entry: './app.js',
  mode: process.env.NODE_ENV
  //...
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From

Difference between production and development build in ReactJS

From Dev

Rails how to switch between dev and production mode?

From Dev

What is the difference between development and production SSL certificates in iOS?

From Dev

What's the difference between preview mode and production mode in LUIS?

From Dev

Can't Set NODE_ENV to production with webpack

From Dev

Can I use NODE_ENV="staging" with mode="production" in webpack 4?

From Dev

"ERROR in window is not defined" webpack general error on webpack --mode=production --env.prod

From Dev

How to read different .env files depending on production or development mode?

From Dev

How to put Vue.js in production mode using webpack 2.7?

From Dev

webpack in production mode doesn't like let

From Dev

Get variable mode development or production in webpack 4

From Dev

How to start foreman in production mode(ENV)

From Dev

Webpack mode production NODE_ENV still undefined

From Dev

switching between debugging and production mode

From Dev

Symfony .env secure passwords in production mode

From Dev

Why Webpack's sass-loader is minimizing CSS in production mode?

From Dev

Webpack -p vs --mode=production

From Dev

How to ignore typescript errors when compile by webpack production mode

From Dev

Material UI / Webpack / React - the className optimization/minification in production mode

From Dev

What happen in production mode if Django crash?

From Dev

Angular @ngtools/webpack build fails when using webpack mode 'production'

From Dev

What is difference between with and env

From Dev

Can't set NODE_ENV=production with npm and webpack

From Dev

How to use webpack Encore to set VueJS production mode

From Dev

Override Webpack 4.25.1's use of Uglify in production mode

From Dev

Webpack 4: Production mode fails for React Library

From Dev

What is different between webpack production and development regarding tree shaking

From Dev

Problem with production mode (Rails 5.2.4.1 + VueJS + Webpack 3.12 + Heroku)

From Dev

What's the difference between auto swap and pushing code directly to production?

Related Related

  1. 1

    Difference between production and development build in ReactJS

  2. 2

    Rails how to switch between dev and production mode?

  3. 3

    What is the difference between development and production SSL certificates in iOS?

  4. 4

    What's the difference between preview mode and production mode in LUIS?

  5. 5

    Can't Set NODE_ENV to production with webpack

  6. 6

    Can I use NODE_ENV="staging" with mode="production" in webpack 4?

  7. 7

    "ERROR in window is not defined" webpack general error on webpack --mode=production --env.prod

  8. 8

    How to read different .env files depending on production or development mode?

  9. 9

    How to put Vue.js in production mode using webpack 2.7?

  10. 10

    webpack in production mode doesn't like let

  11. 11

    Get variable mode development or production in webpack 4

  12. 12

    How to start foreman in production mode(ENV)

  13. 13

    Webpack mode production NODE_ENV still undefined

  14. 14

    switching between debugging and production mode

  15. 15

    Symfony .env secure passwords in production mode

  16. 16

    Why Webpack's sass-loader is minimizing CSS in production mode?

  17. 17

    Webpack -p vs --mode=production

  18. 18

    How to ignore typescript errors when compile by webpack production mode

  19. 19

    Material UI / Webpack / React - the className optimization/minification in production mode

  20. 20

    What happen in production mode if Django crash?

  21. 21

    Angular @ngtools/webpack build fails when using webpack mode 'production'

  22. 22

    What is difference between with and env

  23. 23

    Can't set NODE_ENV=production with npm and webpack

  24. 24

    How to use webpack Encore to set VueJS production mode

  25. 25

    Override Webpack 4.25.1's use of Uglify in production mode

  26. 26

    Webpack 4: Production mode fails for React Library

  27. 27

    What is different between webpack production and development regarding tree shaking

  28. 28

    Problem with production mode (Rails 5.2.4.1 + VueJS + Webpack 3.12 + Heroku)

  29. 29

    What's the difference between auto swap and pushing code directly to production?

HotTag

Archive