Last modified: Jul 20, 2025 By Alexander Williams
Install Webpack in Node.js - Quick Guide
Webpack is a powerful module bundler for JavaScript applications. It helps bundle assets like scripts, styles, and images. This guide will show you how to install Webpack in Node.js.
Prerequisites
Before installing Webpack, ensure you have Node.js and npm installed. You can check their versions using the following commands:
node -v
npm -v
If not installed, download Node.js from the official website. It includes npm by default.
Initialize a Node.js Project
First, create a new project folder and initialize it with npm. Run the following command in your terminal:
mkdir webpack-demo
cd webpack-demo
npm init -y
This creates a package.json file. It stores project dependencies and scripts.
Install Webpack
Install Webpack and its CLI tool as dev dependencies. Use the following command:
npm install webpack webpack-cli --save-dev
This installs Webpack locally in your project. Verify the installation by checking the version:
npx webpack --version
Configure Webpack
Create a webpack.config.js file in your project root. This file defines how Webpack bundles your code. Here's a basic example:
const path = require('path');
module.exports = {
entry: './src/index.js', // Entry point
output: {
filename: 'bundle.js', // Output file
path: path.resolve(__dirname, 'dist'), // Output directory
},
};
This config tells Webpack to bundle index.js from the src folder. The output will be saved as bundle.js in the dist folder.
Create Sample Files
Create a src folder and add an index.js file. Add some sample code:
console.log('Hello, Webpack!');
Run Webpack
Add a build script to your package.json to run Webpack easily:
"scripts": {
"build": "webpack"
}
Now, run the build script:
npm run build
Webpack will bundle your code. Check the dist folder for the output.
Loaders and Plugins
Webpack supports loaders and plugins for advanced functionality. For example, to bundle CSS, install css-loader and style-loader:
npm install css-loader style-loader --save-dev
Then, update your webpack.config.js:
module.exports = {
module: {
rules: [
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
],
},
};
This setup allows Webpack to process CSS files. For more advanced setups, check our guide on installing Babel-ESLint.
Development Mode
Webpack has a development mode for easier debugging. Update your config:
module.exports = {
mode: 'development',
};
This enables source maps and other dev-friendly features. For production, use mode: 'production'.
Using Webpack Dev Server
For live reloading, install webpack-dev-server:
npm install webpack-dev-server --save-dev
Add a start script to package.json:
"scripts": {
"start": "webpack serve"
}
Run the server:
npm start
This starts a development server at http://localhost:8080. For more tools, see our guide on installing Nodemon.
Conclusion
Webpack is a versatile tool for bundling JavaScript applications. This guide covered installation, basic configuration, and common use cases. For more advanced setups, explore loaders and plugins.
If you're working with React, check our guide on installing React in Node.js. Happy bundling!