| title | description |
|---|---|
Installation |
Install webpack and start bundling your JavaScript modules |
This guide covers how to install webpack in your project.
webpack requires **Node.js version 10.13.0 or higher**. Check your Node.js version with `node -v`.Before installing webpack, make sure you have:
- Node.js installed (version 10.13.0 or higher)
- npm, yarn, or pnpm package manager
For most projects, we recommend installing webpack locally. This allows different projects to use different versions of webpack and makes your build reproducible.
If you haven't already, create a `package.json` file:```bash
npm init -y
```
<CodeGroup>
```bash npm
npm install --save-dev webpack webpack-cli
```
```bash yarn
yarn add webpack webpack-cli --dev
```
```bash pnpm
pnpm add -D webpack webpack-cli
```
</CodeGroup>
<Info>
`webpack-cli` is optional but recommended. It provides a command-line interface for running webpack and accessing helpful commands.
</Info>
```bash
npx webpack --version
```
You should see the webpack version number (currently 5.105.4).
If you need webpack available globally (for quick experiments or testing):
```bash npm npm install --global webpack webpack-cli ```yarn global add webpack webpack-clipnpm add -g webpack webpack-cliAfter global installation, you can run webpack directly from your terminal.
To install a specific version of webpack:
```bash npm npm install --save-dev webpack@5.105.4 webpack-cli ```yarn add webpack@5.105.4 webpack-cli --devpnpm add -D webpack@5.105.4 webpack-cliWebpack's power comes from its ecosystem of loaders and plugins. Install them as dev dependencies:
# Example: Installing loaders for CSS and TypeScript
npm install --save-dev css-loader style-loader ts-loader
# Example: Installing common plugins
npm install --save-dev html-webpack-plugin mini-css-extract-pluginTo install the latest development version directly from the repository:
npm install --save-dev webpack/webpack#main webpack-cliAfter installation, verify everything is set up correctly:
-
Check webpack version:
npx webpack --version
-
Check webpack-cli version:
npx webpack-cli --version
-
View webpack help:
npx webpack --help
Add webpack to your package.json scripts for easier access:
{
"name": "my-webpack-project",
"version": "1.0.0",
"scripts": {
"build": "webpack",
"build:prod": "webpack --mode production",
"build:dev": "webpack --mode development",
"watch": "webpack --watch"
},
"devDependencies": {
"webpack": "^5.105.4",
"webpack-cli": "^6.0.1"
}
}Now you can run:
npm run build # Build with default configuration
npm run build:prod # Build for production
npm run build:dev # Build for development
npm run watch # Watch for changes and rebuildNow that webpack is installed, you're ready to:
Create your first webpack bundle Learn how to configure webpack For production builds, consider installing `terser-webpack-plugin` (included by default in webpack 5) for JavaScript minification.