Back to glossary

Bundler (JavaScript)

A JavaScript bundler follows every import from an entry file and emits a few optimized files that contain the app's code and its dependencies.

Also known as JavaScript bundler, module bundler, JS bundler, webpack, build bundler

A JavaScript bundler, also called a module bundler, is a build tool that starts at an entry file, follows every import and require to build a module graph, and writes out a few optimized files containing the application and its dependencies. webpack, Vite, esbuild, and Rollup are bundlers. Bundling erases package boundaries, so bundled dependencies disappear from lockfiles and SBOMs.

That last property is the one security teams miss. A bundle is the artifact that actually ships to browsers, serverless functions, and npm, and it is the one artifact no lockfile describes. We think dependency inventory has to be built from what the bundler consumed and what the tarball contains, not from what a manifest declares.

This page covers JavaScript bundlers, including webpack. Ruby's dependency manager is also called Bundler; it is covered on our RubyGems page.

What is a JavaScript bundler?

#

A JavaScript bundler is a compiler for module graphs: it takes many source files and packages and emits a small number of files a runtime can load directly. It exists because the way developers write JavaScript and the way runtimes want to receive it have never matched.

Developers write hundreds of small modules and import packages by bare name, as in import escapeHtml from "escape-html". A browser cannot resolve "escape-html" on its own, because it has no node_modules directory and no package resolution algorithm. Even with native ES modules and import maps, loading a deep tree one file per request is slow, and shipping a whole library when the app uses one function wastes bytes. Bundlers solve all three: they resolve names at build time, concatenate modules into chunks, and drop code nothing reaches.

The closest analogy is static linking in C. A bundler copies the code of every dependency it reaches into the output, so the result runs without the dependencies being installed. Server code gets bundled for the same reason: a CLI or a serverless function that ships as one file starts faster and does not need npm install at deploy time.

How a bundler builds the module graph

#

Every bundler runs the same five stages, whatever its configuration language.

  1. Resolve. Starting from the entry file, the bundler turns each import specifier into a file path. For bare names it walks up to node_modules, reads the package's package.json, and picks a file using the exports, module, browser, or main fields and the active conditions, such as import, require, or browser.
  2. Transform. Each file is loaded and converted to plain JavaScript: TypeScript and JSX are compiled, CSS and images become modules, and plugins rewrite code. In webpack these are loaders and plugins; in Rollup, Rolldown, and Vite they are plugins with hooks such as resolveId, load, and transform.
  3. Link. Modules are combined into chunks. Bundlers either hoist everything into one scope or wrap each module in a function, which is how CommonJS packages keep their own module.exports.
  4. Tree-shake. Exports nothing imports are removed. Packages that set "sideEffects": false let the bundler drop whole files.
  5. Minify and emit. Identifiers are shortened, whitespace is removed, and the bundler writes chunks plus optional source maps. Our minification page covers what that step destroys.

The figure follows one build. Before it, the source tree holds src/index.js, the dependency's code at node_modules/escape-html/index.js, and the dependency's identity in node_modules/escape-html/package.json. The bundler resolves the import, builds a two-node module graph, and then transforms, links, tree-shakes, and minifies it. The published tarball contains a package.json with no dependencies and one 1,039-byte file, dist/index.js, with escape-html's code inlined and unnamed. npm ls --all and npm sbom both report only greet-demo@1.0.0.

Here is the unminified output of that build with esbuild 0.28.2. The wrapper is how esbuild links a CommonJS package into an ES module bundle, and the path comment is the last trace of where the code came from:

JavaScript
// node_modules/escape-html/index.js
var require_escape_html = __commonJS({
  "node_modules/escape-html/index.js"(exports, module) {
    "use strict";
    var matchHtmlRegExp = /["'&<>]/;
    module.exports = escapeHtml;
    // ...
  }
});

// src/index.js
var import_escape_html = __toESM(require_escape_html());
function render(name) {
  return `<p>Hello, ${(0, import_escape_html.default)(name)}</p>`;
}
export { render };

Add --minify and the path comments go. esbuild moves the package's license header to a /*! Bundled license information */ block at the end of the file by default, and --legal-comments=none removes that too. What remains is 1,039 bytes of code with no name, version, or path in it.

webpack, Rollup, esbuild, and Vite compared

#

webpack, first published to npm in March 2012, made bundling the default for front-end work. Its configuration file, webpack.config.js, is a Node program, and its runtime wraps every module in a function registry called __webpack_require__. Rollup popularized ES module output and tree shaking for libraries. esbuild, written in Go, showed how much faster a bundler compiled to native code could be. Vite started as a dev server on top of esbuild and Rollup, and as of November 2026, Vite 8.3.7 depends on Rolldown, a Rust bundler with a Rollup-compatible plugin API, and no longer on Rollup or esbuild. We covered that shift in the Rolldown-Vite preview.

For supply chain purposes the difference that matters is how much code each bundler pulls into your build machine. We installed each one into an empty project on November 9, 2026, with install scripts disabled, and counted packages with npm ls --all:

BundlerImplementationPackages installedNotes
webpack 5.112.2 + webpack-cli 7.3.1JavaScript77Loaders and plugins add more per project
Vite 8.3.7JavaScript over Rust (Rolldown)15Plugins add more per project
Rollup 4.68.2JavaScript plus a native parser3Platform binary via optionalDependencies
Rspack 2.3.1Rust, webpack-compatible API3Platform binary via optionalDependencies
esbuild 0.28.2Go2Platform binary; also declares a postinstall script

Verdict: pick the bundler on build needs, but count its dependency tree as production attack surface, because everything in it runs on the machine that produces your release. A smaller tree is less code that can be compromised. A prebuilt binary is less code you can read: esbuild 0.28.2 lists 26 platform packages in optionalDependencies, Rollup 4.68.2 lists 27, and Rspack ships its Rust core the same way. That is the native code trade: fewer packages, opaque contents.

Where do bundled dependencies go?

#

Bundled dependencies go into the output file as anonymous code, and every tool that inventories dependencies from metadata stops seeing them. We reproduced this with the build above. We packed the output as greet-demo@1.0.0, with a package.json that declares no dependencies, and installed the tarball into a fresh project with npm 10.9.9:

Bash
$ npm ls --all
consumer@1.0.0 /home/claude/demo/bundler/consumer
`-- greet-demo@1.0.0

$ npm sbom --sbom-format cyclonedx | jq -r '.components[] | "\(.name)@\(.version)"'
greet-demo@1.0.0

$ node -e "import('greet-demo').then(m => console.log(m.render('<b>x</b>')))"
<p>Hello, &lt;b&gt;x&lt;/b&gt;</p>

The escape-html code runs, and nothing in the dependency tree, the lockfile, or the SBOM says it is there. If escape-html 1.0.3 had a vulnerability or a malicious payload, an advisory lookup against this project would come back clean. Our analysis of the axios compromise makes the same point about published packages: vendored or bundled code is not visible as a direct dependency and may not appear in standard dependency trees.

npm has an older mechanism with a similar name, bundleDependencies, and it behaves very differently. It copies whole package directories, package.json included, into the tarball. We packed the same dependency that way and installed it:

Bash
$ npm ls --all
consumer2@1.0.0 /home/claude/demo/bundler/consumer2
`-- greet-bundled-deps@1.0.0
  `-- escape-html@1.0.3

The lockfile records the entry as "version": "1.0.3", "inBundle": true, so scanners still see it.

How the dependency shipsCode in tarballName and version in tarballSeen by npm ls, lockfile, SBOMSeen by content analysis
Normal dependencyNo, installed separatelyYes, in package.jsonYesYes
bundleDependenciesYes, as a directoryYes, its own package.jsonYes, marked inBundleYes
Inlined by a bundlerYes, as anonymous codeOnly if comments or source maps surviveNoOnly if the scanner reads the code

Verdict: when you publish a library, declare dependencies normally or use bundleDependencies, and reserve inlining for applications you deploy yourself. When you consume packages, do not treat a manifest with no dependencies as evidence of no third-party code. This is why software composition analysis that only reads manifests and lockfiles misses bundled code, and why we analyze the files inside every package. Our Minified code alert flags packages that ship minified files, because minified code can hide malicious content from review.

Does disabling install scripts protect a bundled app?

#

Disabling install scripts does not protect a bundled app, because bundled malware runs when the bundle runs, not when the package installs. npm 12 and pnpm 10 now leave dependency install scripts off by default, which fixed a long-standing design mistake and stops most install-time attacks. A payload that only executes inside the shipped bundle never needed an install script.

The Qix compromise on September 8, 2025 is the clearest case. After a maintainer account takeover via a phishing email, malicious versions of chalk, debug, and roughly 20 related packages went out, with the first two waves published between 13:12 and 13:20 UTC. Those packages generally receive 2 to 3 billion downloads a week. The payload first checked typeof window !== 'undefined', then hooked window.fetch, XMLHttpRequest, and window.ethereum.request to swap cryptocurrency recipient addresses. It did nothing in a terminal or a CI job. It did its work only after a bundler copied it into a web application and a user loaded the page.

The 2018 event-stream incident shows the bundler being targeted on purpose. npm's postmortem describes flatmap-stream@0.1.1, added to event-stream@3.3.6 on September 9, 2018. Its encrypted payload used the description of the package that imported it as the decryption key, so it only decrypted inside the Copay wallet's build, and it then wrote a second payload into a dependency the build scripts would package. As npm put it, "running the payload in any other environment has no effect." Copay shipped it in versions 5.0.2 through 5.1.0 before npm removed both packages on November 26, 2018.

Both attacks rely on the bundler doing its job faithfully: it copies whatever is in node_modules into the artifact users run. A bundler has no notion of which code is trusted.

The bundler runs with your build's privileges

#

The bundler itself is a dependency, and it runs before anything ships. webpack.config.js, vite.config.ts, and rollup.config.mjs are programs that Node executes, and every plugin they load runs with the same access as the build: source code, environment variables, and whatever CI secrets the job was given. A compromised loader or plugin does not need an install script either; it runs every time you build.

The build also decides what ships. A plugin can add, rewrite, or drop modules in the graph, so the bundle is only as trustworthy as every package that touched it during the build. Reviewing the source repository tells you what the developer wrote, not what the bundler emitted.

What should change

#

Bundlers already know exactly what they inlined. esbuild's --metafile records each input and how many bytes of it reached the output:

JSON
{
  "outputs": {
    "dist/m.js": {
      "inputs": {
        "node_modules/escape-html/index.js": { "bytesInOutput": 395 },
        "src/index.js": { "bytesInOutput": 69 }
      }
    }
  }
}

That is a path, not a package identity. The bundler has the package.json for every one of those paths open during resolution, and throws the name, version, and integrity away. We think bundlers should emit them, as a build-time SBOM of what was actually inlined, and that libraries published to npm with inlined third-party code should ship that record in the tarball. Until they do, the only reliable inventory of a bundle comes from reading the bundle.