{"id":18005,"library":"webpack-cli","title":"webpack CLI","description":"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.","status":"active","version":"7.0.2","language":"javascript","source_language":"en","source_url":"https://github.com/webpack/webpack-cli","tags":["javascript","webpack","cli","scaffolding","module","bundler","web","typescript"],"install":[{"cmd":"npm install webpack-cli","lang":"bash","label":"npm"},{"cmd":"yarn add webpack-cli","lang":"bash","label":"yarn"},{"cmd":"pnpm add webpack-cli","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core bundler functionality.","package":"webpack","optional":false},{"reason":"Required for the `--analyze` flag.","package":"webpack-bundle-analyzer","optional":true},{"reason":"Required for the `serve` command.","package":"webpack-dev-server","optional":true}],"imports":[{"note":"webpack-cli is primarily a CLI tool; direct programmatic runtime imports are generally not intended. This import is useful for type declarations when extending or analyzing webpack-cli's structure in TypeScript projects.","wrong":"import { WebpackCLI } from 'webpack-cli';","symbol":"WebpackCLI","correct":"import type { WebpackCLI } from 'webpack-cli';"},{"note":"Use this import for type declarations to define expected arguments when programmatically interacting with or extending webpack-cli commands within TypeScript.","wrong":"import { CommandArgs } from 'webpack-cli';","symbol":"CommandArgs","correct":"import type { CommandArgs } from 'webpack-cli';"},{"note":"Provides type definitions for the various options accepted by webpack-cli, valuable for strong type-checking in configuration files or custom scripts.","wrong":"import { WebpackCLIOptions } from 'webpack-cli';","symbol":"WebpackCLIOptions","correct":"import type { WebpackCLIOptions } from 'webpack-cli';"}],"quickstart":{"code":"import path from 'path';\nimport { fileURLToPath } from 'url';\n\n// Get __dirname equivalent for ES modules\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = path.dirname(__filename);\n\nexport default {\n  mode: 'development',\n  entry: './src/index.js',\n  output: {\n    filename: 'bundle.js',\n    path: path.resolve(__dirname, 'dist'),\n  },\n  // Example of using an environment variable from webpack-cli --env\n  // For example: npx webpack-cli build --env API_URL=http://localhost:3000\n  plugins: [\n    {\n      apply: (compiler) => {\n        compiler.hooks.environment.tap('LogEnvPlugin', () => {\n          console.log('API_URL from --env:', process.env.API_URL || 'Not set');\n        });\n      },\n    },\n  ],\n};\n\n// --- src/index.js ---\n// console.log('Hello from webpack!');\n\n// --- Terminal Command ---\n// npx webpack-cli build --env API_URL=https://api.example.com","lang":"typescript","description":"Demonstrates a basic webpack build using `webpack-cli` with a simple ES module configuration, including how to pass environment variables."},"warnings":[{"fix":"Upgrade your Node.js environment to version 20.9.0 or newer. Use `nvm install 20 && nvm use 20` or your preferred Node.js version manager.","message":"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.","severity":"breaking","affected_versions":">=7.0.0"},{"fix":"For new projects or scaffolding, use `npx create-webpack-app` instead. If you need to migrate existing scripts, adapt them to manually configure webpack or use `create-webpack-app`'s functionality.","message":"The `init`, `loader`, and `plugin` commands were removed in webpack-cli v6.0.0. They have been superseded by `create-webpack-app`.","severity":"breaking","affected_versions":">=6.0.0"},{"fix":"Update your `webpack-dev-server` dependency to `^5.0.0` or later in your `package.json`.","message":"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.","severity":"breaking","affected_versions":">=6.0.0"},{"fix":"Ensure your `webpack` dependency is updated to `^5.82.0` or newer in your `package.json`.","message":"The minimum supported `webpack` version for webpack-cli v6.0.0 and higher is `5.82.0`.","severity":"breaking","affected_versions":">=6.0.0"},{"fix":"Update your CLI commands or scripts to use `--config-node-env` instead of the old option name.","message":"The `--define-process-env-node-env` option was renamed to `--config-node-env` in webpack-cli v6.0.0.","severity":"breaking","affected_versions":">=6.0.0"},{"fix":"For optimal compatibility and to utilize modern Node.js features, consider migrating your `webpack.config.js` to an ES module format (e.g., using `export default { ... }`) or naming it `webpack.config.mjs`. If sticking with CJS, ensure it's compatible with potential dynamic import fallbacks or use `--disable-interpret` if encountering issues.","message":"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.","severity":"gotcha","affected_versions":">=7.0.0"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"Install webpack: `npm install --save-dev webpack` or `yarn add webpack --dev`.","cause":"The 'webpack' package is not installed as a peer dependency.","error":"Error: Cannot find module 'webpack'"},{"fix":"Use `npx create-webpack-app` for scaffolding new webpack projects or migrating existing ones. Consult the `webpack-cli` documentation for alternative commands.","cause":"The `init` command (and `loader`, `plugin`) was removed in webpack-cli v6.0.0.","error":"Unknown command 'init'"},{"fix":"Upgrade your Node.js to version 20.9.0 or newer. Recommended: `nvm install 20 && nvm use 20`.","cause":"Your Node.js version does not meet the minimum requirement for webpack-cli v7+.","error":"Node.js vX.Y.Z is not supported. Please use Node.js v20.9.0 or higher."},{"fix":"Install the correct version: `npm install --save-dev webpack-dev-server@^5.0.0` or `yarn add webpack-dev-server@^5.0.0 --dev`.","cause":"webpack-dev-server is missing or an incompatible version is installed when using the `serve` command.","error":"Error: Cannot find module 'webpack-dev-server' or Peer dependency 'webpack-dev-server@^5.0.0' not installed"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}