TExcelTAD

From npm install to dist/

Every JavaScript project you'll touch at work goes through the same two machines before it reaches a user. A package manager (npm, Yarn or pnpm) turns a list of names and version ranges into a concrete tree of files on disk, and records exactly what it chose. A bundler (esbuild, Rollup, Webpack, or Vite on top of them) follows the import statements from your entry file, pulls in what is actually used, and writes a few optimised files into dist/. This course takes both machines apart.

Who this course is for

You can write modern JavaScript, run a script with Node and have typed npm install before. The Node.js course lessons Node.js NPM and package.json & Scripts are the background; this course starts where they stop. TypeScript appears in the library lessons, so the TypeScript course helps, and the Vite course is the natural companion for application builds: here Vite gets one comparison lesson, not a second course.

The whole journey in one session

Here is everything in miniature: a project file, a dependency from a registry, a lockfile, a script, and a bundle.

bash · try it yourself
mkdir -p hello && cd hello
npm init -y > /dev/null
npm pkg set type=module
npm install ms@2.1.3
npm install -D esbuild@0.28.2 2>&1 | grep -v install-scripts
cat > index.js <<'JS'
import ms from 'ms';
console.log(`1 day = ${ms('1d')} milliseconds`);
JS
npm pkg set scripts.build="esbuild index.js --bundle --platform=node --outfile=dist/app.js"
node index.js
npm run build
node dist/app.js
ls node_modules

added 1 package in 444ms

added 2 packages in 2s
1 day = 86400000 milliseconds

> hello@1.0.0 build
> esbuild index.js --bundle --platform=node --outfile=dist/app.js


  dist/app.js  4.3kb

⚡ Done in 3ms
1 day = 86400000 milliseconds
@esbuild
esbuild
ms

What happened, line by line:

Step What the tool did
npm init -y wrote a default package.json (the project's manifest)
npm install ms@2.1.3 asked the registry for ms, downloaded its tarball, unpacked it into node_modules/ms, added "ms": "^2.1.3" to dependencies and wrote package-lock.json
npm install -D esbuild the same for a dev dependency; esbuild also pulled in its platform binary package @esbuild/linux-x64
npm run build ran the build script with node_modules/.bin on the PATH
esbuild followed import ms from 'ms', copied the code of ms into one file, dist/app.js, which runs without node_modules

The bundle is self-contained: dist/app.js would run on a machine with no node_modules at all. That's the job split this course is about: the package manager builds the tree on disk, the bundler flattens the parts you use into shippable files.

How the sessions in this course run

Every bash session is a real terminal session recorded in a Linux container without network access:

Tool Version
Node.js 24.22.1 LTS
npm 11.19.1
pnpm 10.34.7 (through Corepack)
Yarn 4.18.1 "Berry", the default yarn; Yarn 1.22.22 "classic" when a project asks for it
esbuild / Rollup / Webpack 0.28.2 / 4.65.0 / 5.112.2 (with webpack-cli 7.2.3), installed per project
TypeScript 5.9.3, installed per project

Instead of the public npm registry, the container runs Verdaccio, a small private registry, at http://localhost:4873/. All three package managers point at it, and you are logged in as the user learner, so publishing works too. It holds a fixed set of packages:

Package Versions
lodash 4.18.1, 4.17.21
chalk 6.0.1, 4.1.2
semver 7.8.5, 6.3.1
is-number 7.0.0, 6.0.0
ms, left-pad, is-odd 2.1.3, 1.3.0, 3.0.1
react, react-dom 19.3.1 and 18.3.1
redux, react-redux 5.0.1, 9.3.0
typescript, tslib, @types/node 5.9.3, 2.8.1, 24.19.2
esbuild, rollup, webpack, webpack-cli 0.28.2, 4.65.0, 5.112.2, 7.2.3

plus everything those depend on. Asking for another version fails with an ETARGET/notarget error, exactly as it would for a version that doesn't exist on npmjs.com. Three more conventions:

  • Each session starts in an empty home directory and creates its files with cat > file <<'JS' heredocs.
  • Anything a session publishes disappears afterwards: every session gets a fresh registry.
  • Timings (added 1 package in 512ms, Done in 4ms) differ on your machine; everything else should match.

Runnable models

Some ideas are easier to see in a few lines of code than in a tool's output. Those lessons use js run examples that you can edit and run in the browser: a semver range matcher, a module resolver, a tiny bundler with tree shaking. They model the real algorithms in simplified form, and the real tool sessions next to them show the behaviour they model. Here's the first one: what "install" means, stripped to its core.

js · try it yourself
// A registry: package name -> version -> its own dependencies.
const registry = {
  app:  { '1.0.0': { chalk: '4.1.2', ms: '2.1.3' } },
  chalk: { '4.1.2': { 'ansi-styles': '4.3.0', 'supports-color': '7.2.0' } },
  'ansi-styles': { '4.3.0': {} },
  'supports-color': { '7.2.0': { 'has-flag': '4.0.0' } },
  'has-flag': { '4.0.0': {} },
  ms: { '2.1.3': {} },
};

// "Install" = walk the graph from the root and write down every package@version once.
function install(name, version, lock = {}) {
  for (const [dep, v] of Object.entries(registry[name][version])) {
    const key = `${dep}@${v}`;
    if (!lock[key]) {
      lock[key] = { requiredBy: `${name}@${version}` };
      install(dep, v, lock);
    }
  }
  return lock;
}

const lock = install('app', '1.0.0');
for (const [pkg, info] of Object.entries(lock)) console.log(pkg.padEnd(22), '<-', info.requiredBy);
console.log(Object.keys(lock).length, 'packages');
chalk@4.1.2            <- app@1.0.0
ansi-styles@4.3.0      <- chalk@4.1.2
supports-color@7.2.0   <- chalk@4.1.2
has-flag@4.0.0         <- supports-color@7.2.0
ms@2.1.3               <- app@1.0.0
5 packages

Real package managers add the hard parts: version ranges instead of exact versions, conflicts when two packages want different versions, deciding where on disk each package goes, and verifying downloads. Each gets its own lesson.

Course map

Part You'll learn
npm package.json, semver, lockfiles, npm ci, scripts, install-script policies, peer dependencies, .npmrc, workspaces, publishing
Yarn Berry Plug'n'Play, nodeLinker, workspaces, yarn why, constraints
pnpm the content-addressable store, symlinked node_modules, strictness, workspace: and catalog:, filtering
Bundlers module graphs, tree shaking, code splitting; esbuild, Rollup and Webpack; dual ESM/CJS packages with types
Practice exercises, then a pnpm monorepo whose library is published to the registry and consumed by an app

Test yourself

What does a package manager write down so that the next install picks exactly the same versions?

  1. The bundle in dist/
  2. A lockfile such as package-lock.json
  3. The .npmrc file
  4. The node_modules/.bin folder

Why does node dist/app.js work in the intro session even though it imports ms?

  1. Node downloads ms on demand
  2. esbuild copied the code of ms into the bundle
  3. dist/ has its own node_modules
  4. ms is built into Node

Where do the course's sessions download packages from?

  1. registry.npmjs.org
  2. A local Verdaccio registry at localhost:4873
  3. GitHub
  4. A folder of tarballs passed with --offline