webpack CLI
raw JSON →webpack-cli is the official command-line interface for webpack, a widely adopted module bundler for modern JavaScript applications. It provides a robust set of commands and options to streamline the configuration and execution of webpack builds. Currently stable at version 7.0.2, it receives regular patch releases, with major version updates often coinciding with Node.js LTS cycles or significant shifts in the webpack ecosystem. A key differentiator is its ability to simplify the initialization and management of complex webpack configurations, offering a flexible approach to project setup that works both with and without a pre-existing `webpack.config.js` file. It consolidates common development tasks such as building, serving with a dev server, and watching for file changes, significantly enhancing developer productivity within the webpack environment.
Common errors
error Error: Cannot find module 'webpack' ↓
npm install --save-dev webpack or yarn add webpack --dev. error Unknown command 'init' ↓
npx create-webpack-app for scaffolding new webpack projects or migrating existing ones. Consult the webpack-cli documentation for alternative commands. error Node.js vX.Y.Z is not supported. Please use Node.js v20.9.0 or higher. ↓
nvm install 20 && nvm use 20. error Error: Cannot find module 'webpack-dev-server' or Peer dependency 'webpack-dev-server@^5.0.0' not installed ↓
npm install --save-dev webpack-dev-server@^5.0.0 or yarn add webpack-dev-server@^5.0.0 --dev. Warnings
breaking webpack-cli v7.0.0 and above requires Node.js version 20.9.0 or higher. Earlier versions of Node.js are no longer supported. ↓
breaking The `init`, `loader`, and `plugin` commands were removed in webpack-cli v6.0.0. They have been superseded by `create-webpack-app`. ↓
breaking webpack-cli v6.0.0 dropped support for `webpack-dev-server@v4`. You must use `webpack-dev-server@v5` or newer for the `serve` command. ↓
breaking The minimum supported `webpack` version for webpack-cli v6.0.0 and higher is `5.82.0`. ↓
breaking The `--define-process-env-node-env` option was renamed to `--config-node-env` in webpack-cli v6.0.0. ↓
gotcha Since webpack-cli v7.0.0, configuration files (`webpack.config.js`) are primarily loaded using dynamic `import()` to leverage Node.js's native ES module and TypeScript support. It falls back to `require()` only if dynamic import fails. This can affect how `module.exports` or CJS-style configurations behave if not explicitly handled. ↓
Install
npm install webpack-cli yarn add webpack-cli pnpm add webpack-cli Imports
- WebpackCLI wrong
import { WebpackCLI } from 'webpack-cli';correctimport type { WebpackCLI } from 'webpack-cli'; - CommandArgs wrong
import { CommandArgs } from 'webpack-cli';correctimport type { CommandArgs } from 'webpack-cli'; - WebpackCLIOptions wrong
import { WebpackCLIOptions } from 'webpack-cli';correctimport type { WebpackCLIOptions } from 'webpack-cli';
Quickstart
import path from 'path';
import { fileURLToPath } from 'url';
// Get __dirname equivalent for ES modules
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export default {
mode: 'development',
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
// Example of using an environment variable from webpack-cli --env
// For example: npx webpack-cli build --env API_URL=http://localhost:3000
plugins: [
{
apply: (compiler) => {
compiler.hooks.environment.tap('LogEnvPlugin', () => {
console.log('API_URL from --env:', process.env.API_URL || 'Not set');
});
},
},
],
};
// --- src/index.js ---
// console.log('Hello from webpack!');
// --- Terminal Command ---
// npx webpack-cli build --env API_URL=https://api.example.com