Installation Guide
Webpack is used to compile JavaScript modules. Once installed, you can interact with webpack either from its CLI or API. If you're still new to webpack, please read through the core concepts and this comparison to learn why you might use it over the other tools that are out in the community.
System Requirements
Node.js Version Requirements
| Webpack Version | Node.js Version |
|---|---|
| Webpack 4.x | 6.x.x or later |
| Webpack 5.x | 10.13.0 or later |
Note: Webpack 5 requires a more recent Node.js version than Webpack 4.
Installation Methods
You can install webpack in two ways:
Local Installation (Recommended)
Installing webpack locally gives you more control over which version you use for a specific project, and avoids conflicts between projects that may require different versions.
npm
mkdir webpack-demo
cd webpack-demo
# npm
npm init -y
npm install webpack webpack-cli --save-dev
yarn
mkdir webpack-demo
cd webpack-demo
# yarn
yarn init -y
yarn add webpack webpack-cli --dev
pnpm
mkdir webpack-demo
cd webpack-demo
# pnpm
pnpm init
pnpm add webpack webpack-cli -D
Global Installation
Global installation is not recommended because it ties you to a single version of webpack across all of your projects.
npm
npm install -g webpack
npm install -g webpack-cli
yarn
yarn global add webpack
yarn global add webpack-cli
pnpm
pnpm add -g webpack
pnpm add -g webpack-cli
Webpack CLI
The webpack-cli package provides a consistent interface for interacting with webpack through the command line. It's required for most webpack functionality.
Version Compatibility
| Webpack Version | webpack-cli Version |
|---|---|
| Webpack 4.x | webpack-cli 3.x or 4.x |
| Webpack 5.x | webpack-cli 4.x or 5.x |
Installation
# For Webpack 4
npm install webpack@4 webpack-cli@4 --save-dev
# For Webpack 5
npm install webpack@5 webpack-cli@5 --save-dev
Quick Start: Creating a New Project
Webpack 5 introduced a new tool called create-webpack-app to quickly scaffold a new webpack project.
Using create-webpack-app (Webpack 5 only)
npx create-webpack-app webpack-demo
cd webpack-demo
This will prompt you with several questions to configure your project:
- Which JS solution do you want to use? (TypeScript, JavaScript)
- Do you want to use webpack-dev-server? (Yes/No)
- Do you want to simplify the creation of HTML files? (Yes/No)
- Do you want to add PWA support? (Yes/No)
- Which CSS solution do you want to use? (CSS only, CSS-in-JS, etc.)
- Do you want to extract CSS for every file? (Only for Production/No)
- Which package manager do you want to use? (npm, yarn, pnpm)
Manual Project Setup
If you prefer to set up your project manually, follow these steps:
mkdir webpack-demo
cd webpack-demo
# Initialize npm
npm init -y
# Install webpack
npm install webpack webpack-cli --save-dev
# Create directory structure
mkdir src dist
# Create entry file
echo 'console.log("Hello webpack");' > src/index.js
# Create HTML file
cat > dist/index.html << 'EOF'
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Webpack Demo</title>
</head>
<body>
<script src="main.js"></script>
</body>
</html>
EOF
Configuration File
As of version 4, webpack doesn't require a configuration file to bundle your project. However, most projects will need a more complex setup, which is why webpack supports a configuration file.
Basic Configuration (Webpack 5 with ES Modules)
// webpack.config.js
import path from "node:path";
import { fileURLToPath } from "node:url";
// In Node.js versions prior to native support for import.meta.dirname,
// derive __dirname from import.meta.url.
// (Node 20.11+ supports import.meta.dirname and import.meta.filename.)
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export default {
entry: "./src/index.js",
output: {
filename: "main.js",
path: path.resolve(__dirname, "dist"),
},
};
Basic Configuration (Webpack 4 / Webpack 5 with CommonJS)
// webpack.config.js
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
filename: 'main.js',
path: path.resolve(__dirname, 'dist')
}
};
Version Comparison: Configuration Files
| Feature | Webpack 4 | Webpack 5 |
|---|---|---|
| Default Format | CommonJS | ES Modules (also supports CommonJS) |
| ECMAScript Modules | Limited support | Full support |
| import.meta.dirname | Not supported | Supported (Node 20.11+) |
| Type Default | module | module (if type in package.json) |
Verifying Installation
After installation, you can verify that webpack is installed correctly by checking the version:
# Check webpack version
npx webpack --version
# Check webpack-cli version
npx webpack-cli --version
Expected output:
webpack 5.x.x
webpack-cli 4.x.x
Webpack CLI Commands
Once installed, you can use various webpack CLI commands:
| Command | Description |
|---|---|
webpack | Run webpack (default command) |
webpack build | Run webpack build |
webpack watch | Watch for file changes |
webpack serve | Run webpack-dev-server |
webpack info | Output system information |
webpack configtest | Validate webpack configuration |
webpack help | Display help |
Basic Usage
# Without configuration file
npx webpack --entry ./src/index.js --output-path ./dist
# With configuration file
npx webpack --config webpack.config.js
# In watch mode
npx webpack --watch
# With specific mode
npx webpack --mode development
npx webpack --mode production
Package.json Scripts
To make working with webpack more convenient, add npm scripts to your package.json:
Webpack 4 Configuration
{
"name": "webpack-demo",
"version": "1.0.0",
"private": true,
"scripts": {
"build": "webpack",
"watch": "webpack --watch"
},
"devDependencies": {
"webpack": "^4.46.0",
"webpack-cli": "^3.3.12"
}
}
Webpack 5 Configuration
{
"name": "webpack-demo",
"version": "1.0.0",
"private": true,
"type": "module",
"scripts": {
"build": "webpack",
"watch": "webpack --watch"
},
"devDependencies": {
"webpack": "^5.88.0",
"webpack-cli": "^5.1.4"
}
}
Version-Specific CLI Features
Webpack 4 CLI
- Basic build and watch commands
- Basic configuration options
- Limited plugin support from CLI
Webpack 5 CLI
- Improved configuration schema from webpack core
- More options available from command line
- Better error messages and diagnostics
- Support for
create-webpack-appscaffolding
Troubleshooting
Common Installation Issues
1. Node.js Version Too Old
Error: Webpack 5 requires Node.js version 10.13.0 or later
Solution: Update Node.js to a supported version
2. Missing webpack-cli
Error: Could not find webpack-cli. Did you mean to install it?
Solution: Install webpack-cli as a dev dependency:
npm install webpack-cli --save-dev
3. Permission Errors
Error: EACCES: permission denied
Solution: Use sudo or fix npm permissions:
# Option 1: Use sudo (not recommended)
sudo npm install -g webpack
# Option 2: Fix npm permissions
sudo chown -R $(whoami) ~/.npm
4. Package Not Found
Error: Cannot find module 'webpack'
Solution: Ensure webpack is installed locally:
npm install webpack --save-dev
Version Conflicts
If you have multiple webpack versions installed globally, you may encounter conflicts:
# Check global packages
npm list -g webpack webpack-cli
# Uninstall conflicting versions
npm uninstall -g webpack
npm uninstall -g webpack-cli
# Install specific version
npm install -g webpack@5
npm install -g webpack-cli@4
Next Steps
After installation, continue with:
- Getting Started Guide: Learn the basics of webpack configuration
- Configuration: Understand webpack's configuration options
- Loaders: Learn how to use loaders for non-JavaScript files
- Plugins: Discover plugins for advancedbuild customized
