Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.webpack.js.org/llms.txt

Use this file to discover all available pages before exploring further.

Webpack provides several CLI commands to build, serve, and initialize your projects.

webpack build

Compiles your webpack project.

Syntax

webpack build [options]

Description

The build command compiles your webpack configuration and outputs bundled assets. This is the default command when running webpack without any subcommand.

Usage Examples

# Basic build
webpack build

# Build with custom config
webpack build --config webpack.prod.js

# Build with mode
webpack build --mode production

# Build and watch
webpack build --watch

Return Value

Exits with code 0 on success, non-zero on error.

webpack serve

Starts a development server with live reloading.

Syntax

webpack serve [options]

Description

The serve command starts webpack DevServer, which provides live reloading and hot module replacement for development.
Requires webpack-dev-server to be installed.

Usage Examples

# Start dev server
webpack serve

# Serve on custom port
webpack serve --port 3000

# Serve with hot reload
webpack serve --hot

# Open browser automatically
webpack serve --open

Parameters

--port
number
Port number for the dev server. Default: 8080
--host
string
Host to use for the dev server. Default: localhost
--hot
boolean
Enable hot module replacement. Default: true
--open
boolean
Open the browser after server starts. Default: false

webpack watch

Watches files and rebuilds on changes.

Syntax

webpack watch [options]

Description

The watch command starts webpack in watch mode. It monitors your source files and automatically rebuilds when changes are detected.

Usage Examples

# Start watch mode
webpack watch

# Watch with specific config
webpack watch --config webpack.dev.js

# Watch with info verbosity
webpack watch --stats verbose

Parameters

--watch-options-stdin
boolean
Stop watching when stdin stream ends. Default: false
--watch-options-poll
number
Polling interval in milliseconds. Use when file watching doesn’t work.

Return Value

Runs continuously until terminated. Exits with code 0 on graceful shutdown.

webpack init

Scaffolds a new webpack project.

Syntax

webpack init [scaffold-template]

Description

The init command helps you create a new webpack configuration interactively. It can use built-in templates or custom scaffolds.
Requires @webpack-cli/generators to be installed.

Usage Examples

# Interactive initialization
webpack init

# Initialize with specific generator
webpack init react-app

# Force initialization (overwrite existing files)
webpack init --force

Parameters

scaffold-template
string
Name of the scaffold template to use. If omitted, uses interactive mode.
--force
boolean
Overwrite existing files without prompting. Default: false
--template
string
Template to generate from. Can be a package name or local path.

Return Value

Exits with code 0 on successful initialization, non-zero on error.

Common Options

These options work with all commands:
--config
string
Path to webpack configuration file. Default: webpack.config.js
webpack build --config config/webpack.prod.js
--mode
string
Build mode: development, production, or none. Sets process.env.NODE_ENV and applies defaults.
webpack build --mode production
--env
object
Environment variables passed to the config function.
webpack build --env production --env apiUrl=https://api.example.com
--stats
string
Stats output preset: none, errors-only, minimal, normal, verbose, detailed.
webpack build --stats detailed
--progress
boolean
Show compilation progress. Default: false
webpack build --progress
--help
boolean
Display help information.
webpack build --help
--version
boolean
Display webpack version.
webpack --version

Exit Codes

CodeDescription
0Success
1Errors from webpack
2Configuration or options error

See Also