Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 38 ยท Getting Started

Build Modes: Development, Production and None

Learn what Webpack's mode option changes: minification, tree shaking, process.env.NODE_ENV, source maps and module names, and how to choose between development, production and none.

Webpack's mode option is a one-line switch that turns on a bundle of sensible defaults. There are three values: development, production and none. Choosing the right one is the first optimization you make, and it explains why the same code can produce a 1 MB readable file in development and a 120 KB compact file in production.

JavaScript
module.exports = {
  mode: 'production', // 'development' | 'production' | 'none'
};

You can set it in the config file or on the command line (webpack --mode production). The command-line value wins over the file.

What each mode turns on

Setting development production none
process.env.NODE_ENV replaced with "development" "production" nothing
Minification (Terser) off on off
Tree shaking (unused exports removed) off on off
Scope hoisting (module concatenation) off on off
Module and chunk names readable paths short numeric ids defaults
devtool default eval (fast rebuilds) none none
Caching in memory on off off

none opts out of every default. Use it when you want to set each option yourself, or when you study the unmodified output.

flowchart LR
  M{"mode"} -->|development| D["fast rebuilds, readable names, eval source maps"]
  M -->|production| P["minify, tree shake, hoist, small ids"]
  M -->|none| N["no defaults"]

The mode is a preset. Every setting it applies can still be overridden.

The NODE_ENV switch

Many libraries check process.env.NODE_ENV to include extra warnings in development. React, for example, prints helpful messages only when the value is not "production". In a browser there is no process, so Webpack replaces the expression with a literal at build time.

After the replacement the condition is a constant, a minifier can see that the dead branch never runs, and it deletes it. That is why production builds of libraries are smaller: the development-only code vanishes.

Node.js Stand-in: replace NODE_ENV and drop dead code

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The real pipeline does this with a proper parser (Terser), and handles all shapes of constant conditions. The lesson to keep: build-time constants let the minifier delete code.

Choosing a mode per build

The usual pattern is development while coding and production when you deploy.

JSON
{
  "scripts": {
    "start": "webpack serve --mode development",
    "build": "webpack --mode production"
  }
}

Do not deploy a development build. It is larger, slower, and may contain debug-only code, internal paths and full source text in eval strings.

If you set mode in the config file but the tool also sets NODE_ENV, remember they are two different things. mode controls what Webpack does; process.env.NODE_ENV inside the compiled app is set by Webpack from mode. Reading process.env.NODE_ENV in your config file gives the shell's value, which is often undefined. Prefer argv.mode in a function config (see The Webpack Configuration File).

Overriding what a mode sets

Because a mode is only a preset, you can change individual options:

JavaScript
module.exports = {
  mode: 'production',
  devtool: 'source-map',            // keep source maps in production
  optimization: {
    minimize: false,                // inspect the production output readably
  },
};

This combination is useful when you debug a problem that only appears in production: you keep the production code path (no eval, tree shaking on) without unreadable output.

Node.js Stand-in: a preset with overrides

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Which mode when?

  • development: daily work, webpack serve, HMR (Hot Module Replacement). Speed and readable errors matter.
  • production: anything users download. Size and speed matter.
  • none: teaching, debugging the bundler itself, or fully custom pipelines.

A common beginner mistake is measuring bundle size in development mode and panicking. Always compare production output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which mode enables minification and tree shaking by default?

    production turns on Terser minification, tree shaking and scope hoisting; development and none do not.
  2. 2. Why does a production build of a library often contain less code?

    Replacing NODE_ENV with a literal lets the minifier prove that development-only branches never run.
  3. 3. You pass --mode production on the command line while the config says mode development. Which wins?

    The CLI flag overrides the value in the config file.
  4. 4. When would you use mode none?

    none applies no defaults, which is useful for custom pipelines or studying the raw output.

Summary

  • mode is a preset: development favors speed and readability, production favors small output, none applies nothing.
  • Webpack replaces process.env.NODE_ENV with a constant, enabling dead-code removal.
  • Production turns on minification, tree shaking and scope hoisting.
  • Any preset value can be overridden, for example devtool or optimization.minimize.
  • Never ship a development build; compare bundle sizes in production mode.

Next: How a Bundler Works