Start your first webpack project

Install webpack, organize your project, write a configuration file, and run your first build.

🎯 Introduction

In this article, you will set up a simple webpack project: install webpack, organize the project structure, write a configuration file, generate a JavaScript bundle, and include it in an HTML page. This gives you a practical starting point for a modern bundling workflow that improves development efficiency and code quality.


🛠️ Install webpack

1. Create an empty project

mkdir webpack-project
cd ./webpack-project

2. Initialize package.json

npm init

After you run npm init, the terminal asks a series of questions. You can press Enter to accept the defaults. This creates a package.json file in the project root, where you can manage the dependencies you install next.

3. Install webpack and its CLI

npm install webpack webpack-cli

webpack bundles modules, and webpack-cli provides its command-line interface. Install them together in this step.


📁 Understand the project structure

Your project can use the following structure:

webpack-project/
├── src/
│   └── index.js
├── dist/
│   └── index.html
├── webpack.config.js
└── package.json
  • src/ contains the source files, including the entry file, index.js.
  • dist/ contains the generated bundles and the HTML file.
  • webpack.config.js tells webpack how to build the project.

Create these directories manually or in your editor, whichever is more convenient.


⚙️ Write your first webpack configuration

Add the following to webpack.config.js in the project root:

const path = require('path');

module.exports = {
    entry: './src/index.js', // Entry point
    output: {
        filename: 'bundle.js', // Output filename
        path: path.resolve(__dirname, 'dist'), // Output path; must be absolute
    },
    mode: 'development',
};

This configuration has three main parts:

  • entry specifies the file where webpack starts bundling.
  • output specifies the filename and path for the generated bundle.
  • mode is set to development for easier debugging. Later, you can use production to optimize the code.
webpack requires output.path to be an absolute path. Use path.resolve to resolve it correctly.

🚀 Set the entry point and output

The entry and output options are central to the configuration. webpack starts at the entry point, recursively resolves dependencies, and generates one or more bundles.

Here, the entry point is src/index.js and the output file is dist/bundle.js.

Create src/index.js and add a simple test:

console.log('Hello Webpack!');

🏗️ Run the build to generate bundle.js

Now run the build.

Add a command to scripts in package.json:

"scripts": {
  "build": "webpack"
}

Then run this command in the terminal:

npm run build

webpack uses your configuration to generate dist/bundle.js.

Open the dist directory to find the generated bundle.js file.


🌐 Include the bundle with a script tag

Finally, create an HTML page and include the bundle with a <script> tag so it can run in the browser.

Add the following to dist/index.html:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>My first webpack project</title>
    </head>
    <body>
        <h1>Welcome to webpack!</h1>
        <script src="bundle.js"></script>
    </body>
</html>

Open dist/index.html in a browser, then open the console. You should see Hello Webpack!.

Your webpack project is now ready!


❓ Questions and answers

Q1: Why do we need webpack-cli?webpack-cli provides webpack's command-line interface. It lets you run builds from the terminal.

Q2: Why must output.path be absolute? webpack needs an absolute path to locate the output directory reliably and avoid path resolution errors.

Q3: How do development and production configurations differ? Development mode (mode: 'development') preserves debugging information, while production mode (mode: 'production') minifies code and optimizes performance.


🧪 Exercises

Exercise 1: Update src/index.js with a function that logs the current time, and call it when the page loads.

Starter code:

function printTime() {
    // Log the current time here
}

printTime();

🎉 You can now install and configure webpack, build a bundle, and load it in a page! Try splitting more of your code into modules to explore a modern frontend development workflow.