Skip to content
Toolsintermediate

What is webpack?

Updated Nov 30, 20263 min readBased on official documentation
What is webpack?

In short

webpack is a static module bundler for JavaScript applications. It reads your project starting from one or more entry files, follows every import, and combines the needed code into one or more bundle files that a browser can load. It is configured through a config file or used with sensible defaults.

How webpack works

When you run webpack, it starts at an entry point, usually a JavaScript file. It reads that file, finds every import it contains, then reads those files and their imports in turn. The result is a dependency graph: a map of every module your project relies on, directly or indirectly.

Once the map is complete, webpack packs the modules into bundles. A bundle is a static file that the browser can download and run. Because the bundles are plain files, you can host them on any web server or content delivery network.

The default entry is ./src/index.js. You can change it by setting an entry property in your configuration, and you can list several entries when your app has more than one page or section.

The main concepts

webpack is built around a few core ideas. Each one has a clear job:

  • Entry tells webpack where to start building the dependency graph.
  • Output tells webpack where to write the bundles and what to name them. By default the main file goes to ./dist/main.js.
  • Loaders teach webpack how to handle file types it does not understand by default. Out of the box it handles JavaScript, JSON and WebAssembly, and it also has built-in support for CSS and HTML.
  • Plugins handle wider tasks, such as optimizing bundles, managing assets, or injecting environment values.
  • Mode switches on a set of built-in optimizations for development or production.

You do not need to learn all of them at once. Most small projects start with entry and output, then add loaders or plugins when a real need appears.

A simple example

Here is a small configuration file that sets an entry, an output name and location, and a loader for JavaScript files that uses babel-loader:

import path from "node:path";
import { fileURLToPath } from "node:url";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

export default {
  entry: "./path/to/my/entry/file.js",
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "my-first-webpack.bundle.js",
  },
  module: {
    rules: [{ test: /\.js$/, use: "babel-loader" }],
  },
};

The rules entry has two required parts. The test property is a pattern that matches file names, and use names the loader that transforms those files before they join the bundle. The path module is a core Node.js module used here to build file paths in a reliable way.

The mode setting controls which built-in optimizations run. It accepts development, production or none, and production is the default.

Common mistakes

A frequent surprise is forgetting that webpack only follows imports. A file that nothing imports, or a script that a page loads on its own, is not part of the graph unless you add it as an entry.

Another common issue is installing a loader but not adding a rule for it. A loader does nothing until a rule's test pattern matches the files it should handle.

Browser support is also worth checking. webpack supports browsers that are ES5-compliant, so Internet Explorer 8 and older are not supported. Because import() and require.ensure() need Promise, older browsers that lack it need a polyfill loaded first.

Finally, webpack 5 needs Node.js 10.13.0 or later to run, so check your Node version before you install it.

When to use webpack

webpack fits best when your front-end code uses modules, many files, or assets like styles and images that must be processed and served together. It is also useful when you want one or more output bundles with predictable names and locations.

For very small scripts with no imports, a single file loaded with a script tag may be enough. Adding a bundler there can add more setup than value.

Alternatives

Other bundlers and build tools exist, and each makes different trade-offs between setup effort, speed and feature set. Compare them against what your project needs before choosing. The official documentation linked below is the best place to confirm the current concepts and options.

FAQ

Does webpack need a configuration file?

No. Since version 4.0.0, webpack can bundle a project without a configuration file, using default values such as ./src/index.js as the entry. A configuration file is useful when you need to customize how the build works.

What is the difference between a loader and a plugin?

A loader transforms individual files so they can become modules in the dependency graph. A plugin works on a wider scale, for tasks like optimizing bundles, managing assets, or injecting environment variables.

What does the mode setting do?

Mode turns on webpack's built-in optimizations for a given environment. You can choose development, production or none, and the default is production.

Which Node.js version does webpack 5 need?

webpack 5 requires Node.js version 10.13.0 or later to run.

Sources

This page is based on the official documentation below. Read it for full detail.

webpack — official documentation ↗
blozhikDeveloper news from across the web, explained. Every story links to its original sources.