Skip to main content

Package Managers: npm, pnpm, Yarn, Bun

A package manager installs the dependencies listed in package.json (as semver ranges) and writes a lockfile with the exact resolved versions, so every machine and CI gets the same tree. The lockfile is committed; CI installs strictly from it (npm ci, pnpm install --frozen-lockfile). npm ships with Node.js. pnpm keeps one copy of each package version in a global store and hard-links it into projects, and its node_modules is strict: a project can only import what it declares. Yarn 4 installs with Plug'n'Play by default, without node_modules. Bun is a JavaScript runtime with a very fast built-in package manager.

Applies to: npm 12, pnpm 12, Yarn 4, Bun 1.4.

Details​

Comparison​

npmpnpmYarn 4Bun
Lockfilepackage-lock.jsonpnpm-lock.yamlyarn.lockbun.lock
node_modulesFlat (hoisted)Symlinks into node_modules/.pnpm, files hard-linked from a global storeNone (PnP) or nodeLinkerHoisted; isolated in new workspaces
Phantom dependenciesPossibleNot possible for your own codeNot possible with PnPPossible when hoisted
Workspacesworkspaces in package.jsonpnpm-workspace.yamlworkspaces in package.jsonworkspaces in package.json
Dependencies' install scriptsOff by default, allowScriptsOff by default, allowBuildsOff by default since 4.14 (enableScripts), dependenciesMetaA built-in allowlist or trustedDependencies

A phantom dependency is a package the code imports without declaring it, which works only because a flat node_modules hoisted it from someone else's dependencies. It breaks when that other package changes.

Commands​

TasknpmpnpmYarnBun
Install everythingnpm installpnpm installyarnbun install
Install in CI, from the lockfilenpm cipnpm install --frozen-lockfileyarn install --immutablebun install --frozen-lockfile
Add a dependencynpm install reactpnpm add reactyarn add reactbun add react
Add a dev dependencynpm install -D vitestpnpm add -D vitestyarn add -D vitestbun add -d vitest
Removenpm uninstall reactpnpm remove reactyarn remove reactbun remove react
Run a scriptnpm run buildpnpm buildyarn buildbun run build
Run a package without installingnpx create-vitepnpm dlx create-viteyarn dlx create-vitebunx create-vite

npm ci deletes node_modules, installs exactly the lockfile and fails if it does not match package.json; npm install may update the lockfile. The --save flag is the default since npm 5.

Semver Ranges​

RangeAllows1.2.3 →
^1.2.3Minor and patch (default)>=1.2.3 <2.0.0
~1.2.3Patch>=1.2.3 <1.3.0
1.2.3Exactly this version1.2.3

For 0.x versions ^ is stricter: ^0.2.3 means >=0.2.3 <0.3.0, and ^0.0.3 allows only 0.0.3.

Dependency Types​

  • dependencies: needed at runtime; installed for the consumers of a library.
  • devDependencies: build, test and lint tools; not installed for consumers. In an app that is bundled, the split is a convention.
  • optionalDependencies: installed when possible; the install does not fail if they cannot be: platform-specific binaries such as @esbuild/*.
  • peerDependencies: the host package must provide it (a React component library declares react), so the app has one copy. npm 7+ and pnpm install missing peers automatically.

Lockfiles​

  • The lockfile of a dependency is ignored: only the root project's lockfile counts. A library is tested against fresh ranges anyway, so its lockfile only pins its own development setup.
  • npm-shrinkwrap.json is a lockfile that is published with a package and respected on install. Meant for CLI tools and other apps distributed through npm, not for libraries.

Pinning the Package Manager​

  • The packageManager field in package.json ("pnpm@12.11.2") declares which manager and version the project uses. pnpm switches to that version by itself; pnpm/action-setup reads the field in CI. The newer devEngines.packageManager field does the same and is read by pnpm and npm.
  • Corepack reads the field and downloads the right Yarn or pnpm on demand. It ships with Node.js up to 24; from Node.js 25 it is no longer bundled and is installed with npm install -g corepack.

Install Scripts​

preinstall, install and postinstall scripts of dependencies run arbitrary code on install, a common supply-chain attack vector. npm 12, pnpm 10+ and Yarn 4.14+ no longer run them by default; the npm 11 bundled with Node.js 24 and 26 still does (upgrade with npm install -g npm@12). Approve the packages that need them (native builds like sharp, binary downloaders like Playwright) and commit the allowlist:

ManagerApproveWhere the allowlist lives
npm 12npm approve-scriptsallowScripts in package.json
pnpmpnpm approve-buildsallowBuilds in pnpm-workspace.yaml (pnpm 11 removed onlyBuiltDependencies)
Yarn 4Edit package.jsondependenciesMeta: { "sharp": { "built": true } }
Bunbun pm trust <package>trustedDependencies in package.json (replaces Bun's built-in list of popular packages)

Example​

package.json
{
"packageManager": "pnpm@12.11.2",
"dependencies": {
"date-fns": "~4.1.0"
},
"devDependencies": {
"react": "^19.3.0"
},
"peerDependencies": {
"react": ">=18"
}
}
pnpm-workspace.yaml
# In minutes: wait a week before installing new versions
minimumReleaseAge: 10080

# Install scripts allowed (true) or explicitly skipped (false)
allowBuilds:
sharp: true
core-js: false

Follow-up Questions​

  • Why is pnpm fast and small on disk? One copy of each version in a content-addressable store, hard-linked into every project; nothing is copied twice.
  • What happens if the lockfile is not committed? Each install resolves the ranges again and can bring different versions: "works on my machine".
  • How to update dependencies? npm update within ranges, npm outdated to see new majors; pnpm: pnpm update, pnpm update --latest, pnpm outdated.
  • How to force the version of a nested dependency? overrides (npm, Bun), resolutions (Yarn), overrides in pnpm-workspace.yaml: a vulnerable transitive package is replaced without waiting for its parents.
  • How do you protect against a malicious new version? Install from the lockfile in CI, keep install scripts blocked and keep a minimum release age: minimumReleaseAge in pnpm (one day by default), min-release-age in npm, npmMinimalAgeGate in Yarn.
  • pnpm dlx or pnpm exec? dlx downloads a package to a temporary location and runs it; exec runs a binary already installed in the project (npx does either).

Common Mistakes​

  • npm install in CI instead of npm ci.
  • Deleting the lockfile to "fix" an install problem: all versions drift at once.
  • Mixing package managers in one repository and committing two lockfiles.
  • A range in packageManager (pnpm@^12): it takes an exact version; a range goes in devEngines.packageManager.

See Also​