Package Managers

Install, version and script your dependencies with npm (recommended), and know when pnpm, Yarn or Bun make sense.

Beginner · about 3 hours · difficulty 1/5

Learn first: JavaScript Modules

A package manager downloads third-party code from the npm registry, records exactly which versions you use and runs project scripts. Every frontend project has a package.json.

Core concepts

  • dependencies ship with your app; devDependencies are build and test tools.
  • Semantic versioning: MAJOR.MINOR.PATCH. ^1.4.2 allows minor and patch updates; ~1.4.2 only patches. Major versions may break things.
  • The lockfile (package-lock.json, pnpm-lock.yaml, yarn.lock, bun.lock) pins the exact resolved tree. Commit it. Use npm ci in CI for clean, reproducible installs.
  • Scripts: "dev", "build", "test", "lint" in package.json, run with npm run build. npx runs a package binary without installing it globally.

Which one?

  • npm (recommended to start): ships with Node, works everywhere, and every tutorial assumes it.
  • pnpm: a content-addressed store with hard links, so it is fast and disk-efficient, and stricter about undeclared dependencies. Excellent for monorepos via workspaces.
  • Yarn: popularised lockfiles and workspaces; modern Yarn (Berry) offers Plug'n'Play.
  • Bun: an all-in-one runtime, package manager, bundler and test runner focused on speed.

Pick one per repo and stick to it; mixing lockfiles causes drift.

Hygiene

Check a package's maintenance, size and weekly downloads before adding it. Run npm audit or enable Dependabot or Renovate, and remember that every dependency is code from strangers running on your machine and your users' browsers.

Move on when you can explain every field in your package.json and recover a broken node_modules with a clean install.

Key concepts

  • npm: Default choice, bundled with Node
  • pnpm: Fast, strict, great for monorepos
  • Yarn
  • Bun: Runtime plus package manager
  • package.json and scripts
  • Semantic versioning and ranges
  • Lockfiles and npm ci
  • npx and binaries
  • Workspaces and monorepos
  • npm audit and dependency updates

Mini project

Initialise a project with npm, add a date library and a dev tool, write dev/build/lint scripts, then recreate the same setup with pnpm and compare install times and node_modules structure.

Check yourself

What does the caret in ^2.3.1 allow, and why should the lockfile still be committed?

Free resources