Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 38 ยท Getting Started

What Is Webpack?

Learn what Webpack is, which problem a module bundler solves, how the dependency graph works and when to use Webpack instead of plain script tags or newer tools.

A modern web app is not one JavaScript file. It is hundreds of small files: components, helpers, stylesheets, images, fonts, and third-party packages from npm. Browsers can load ES modules directly, but doing that with hundreds of files and a deep package tree is slow, and browsers cannot import a .css file, a TypeScript file or a JSX file at all.

Webpack is a module bundler. You give it an entry file, it follows every import and require it finds, builds a map of everything your app needs, and writes one or more optimized files (bundles) that a browser can run. Along the way it can transform files: compile TypeScript, process CSS, hash file names, and remove code you never use.

The examples in this course show real Webpack configuration as read-only code, because the site cannot run Webpack itself. Every runnable example is a small, honest stand-in written in plain Node.js that shows the idea Webpack uses (dependency graph, module wrapper, plugin hooks). The lessons always say which is which.

The problem: scripts, globals and order

Before bundlers, you wrote one <script> tag per file and hoped the order was right. Every top-level variable became a global, so two files could silently overwrite each other.

HTML
<script src="jquery.js"></script>
<script src="utils.js"></script>
<script src="cart.js"></script>   <!-- needs utils.js -->
<script src="app.js"></script>    <!-- needs cart.js and utils.js -->

If cart.js defines var total and app.js defines it again, one of them breaks. A bundler solves this with modules: each file has its own scope and states what it needs with import. The tool, not you, works out the order.

Node.js Stand-in: globals versus module scope

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Webpack wraps every file in a function like the one in the second half, which is why variables never collide.

The dependency graph

Starting from the entry, Webpack reads a file, finds its imports, reads those files, and keeps going until nothing new turns up. The result is a dependency graph: nodes are modules, edges are imports.

flowchart LR
  A["src/index.js"] --> B["src/cart.js"]
  A --> C["styles.css"]
  B --> D["src/utils.js"]
  A --> D
  D --> E["node_modules/lodash"]
  A --> F["logo.png"]

Everything reachable from the entry ends up in the output; everything else is ignored.

The next TryIt builds a very small version of that graph from text files stored in memory. It uses a regular expression, which is a simplification: Webpack uses a real JavaScript parser.

Node.js Stand-in: discover the dependency graph

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

src/unused.js exists on disk but nothing reaches it, so it is not bundled.

What Webpack adds on top of bundling

Bundling is the core, but Webpack is built around four ideas that later chapters cover:

On top of those: a dev server with hot reloading, code splitting, tree shaking, content hashing for long-term caching, and a very large ecosystem.

When should you use Webpack?

Webpack is a strong choice when you need fine control: complex asset pipelines, module federation, custom loaders, or an existing large codebase. Many frameworks (older Create React App, Angular CLI in the past, Next.js in the past) used it under the hood. Newer tools such as Vite, esbuild and Rspack offer faster startup or a simpler setup, and the chapter Migration and Alternatives compares them honestly. Knowing Webpack is still valuable: its concepts (graph, loaders, plugins, chunks) appear in every bundler.

Plain <script type="module"> is fine for small sites and prototypes. Reach for a bundler when you use npm packages, need TypeScript or JSX, want smaller downloads, or ship to many browsers.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does Webpack start from when it builds the dependency graph?

    Webpack begins at the entry, follows imports, and bundles only what is reachable.
  2. 2. Why do bundled modules not overwrite each other's variables?

    Each module is wrapped in a function, so top-level variables are local to that module.
  3. 3. A file src/unused.js is never imported. What happens to it?

    Only modules reachable from an entry become part of the graph and the output.
  4. 4. Which of these is NOT one of Webpack's core ideas?

    Entry, output, loaders, plugins and mode are the core concepts; there is no database.

Summary

  • Webpack is a module bundler: it follows imports from an entry and writes optimized bundles.
  • Modules give every file its own scope, so globals and script order stop being a problem.
  • The dependency graph decides what is included; unreachable files are dropped.
  • Loaders handle non-JS files, plugins hook into the build, and mode switches defaults.
  • Newer tools are faster to start, but Webpack's concepts transfer everywhere.

Next: Install Webpack and Build Your First Bundle