pnpm vs npm for Lockfiles, Monorepos and CI
Both install from the same registry and both lock the tree. pnpm keeps one store per machine and lets a package see only what it declares; npm ships with Node.js and, since npm 12, blocks unapproved install scripts too.
By Build Stack Review · Last reviewed
The short answer
Pick pnpm for a monorepo, for machines and CI runners that install many projects, or when you want the layout itself to catch imports nobody declared: its isolated node_modules, shared store, workspace: protocol, filters and catalogs are built for that. Stay on npm when a single app already installs the same way every time with npm ci and you don't want a second tool to install and pin: npm is already on every machine that has Node.js.
The safety gap has narrowed. npm 12 skips dependency install scripts that allowScripts hasn't approved and turns off git and tarball-URL dependencies by default; pnpm 11 and later fail the install on unapproved build scripts and wait a day before resolving a new release. Whichever you use, commit the lockfile and install from it frozen in CI.
pnpm
A package manager that keeps each package file once in a content-addressable store and links it into every project that uses it. Its default node_modules puts only your direct dependencies at the top; everything else sits behind symlinks in node_modules/.pnpm.
npm
The package manager bundled with Node.js. By default it hoists dependencies into a flat node_modules, records the resolved tree in package-lock.json, and reinstalls exactly that tree with npm ci.
pnpm and npm side by side
| pnpm | npm | |
|---|---|---|
| node_modules layout | Isolated by default: direct dependencies at the top level, the rest symlinked from node_modules/.pnpm. nodeLinker: hoisted builds an npm-style flat tree instead. | Hoisted by default: dependencies flattened to the top level wherever versions allow. install-strategy=linked builds an isolated tree (stable since npm 12). |
| Undeclared ("phantom") imports | An import of a package your package.json doesn't list usually fails, because nothing put it in your node_modules. | Often works by accident when a transitive dependency is hoisted to the top, then breaks when an upgrade moves it. npm's docs suggest linked to catch this. |
| Disk use across projects | One store per machine (per drive). Files are hard-linked or cloned into each project, so a version many projects use is kept once. | Every project gets its own copy in node_modules. The cache under ~/.npm saves downloads, not disk space. |
| Lockfile | pnpm-lock.yaml, one per workspace by default. | package-lock.json. As of npm 12, npm-shrinkwrap.json is no longer read. |
| Frozen installs in CI | In CI with a lockfile present, pnpm install turns on --frozen-lockfile by itself and fails if the lockfile is out of date. | npm ci removes node_modules, installs exactly the lockfile, and fails if it doesn't match package.json. Plain npm install can rewrite the lock. |
| Workspaces | pnpm-workspace.yaml, --filter selectors (by name, dependents, dependencies or changed files), the workspace: protocol, and catalogs that share one version range across packages. | workspaces in package.json with the -w and --workspaces flags. Local packages link by name and version range; there is no workspace: protocol and no catalogs. |
| Dependency install scripts | Run only for packages allowed in allowBuilds (pnpm approve-builds writes it). Since pnpm 11, an unapproved build script fails the install. | Since npm 12, skipped unless approved in allowScripts (npm install-scripts approve), with a warning listing what was skipped; strict-allow-scripts makes it an error. npm 11 and older run them. |
| Brand-new releases | minimumReleaseAge defaults to 1440 minutes (one day) from pnpm 11, so a version published in the last day isn't resolved. | min-release-age (in days) and before (a date) are available, off by default. |
| Git and tarball dependencies | blockExoticSubdeps, on by default from pnpm 11, stops transitive dependencies coming from git repos or tarball URLs. | From npm 12, allow-git and allow-remote default to none; a project opts back in. |
| Pinning the tool itself | Reads packageManager (or devEngines.packageManager) and by default downloads and runs the version the project declares. It can also pin Node.js through devEngines.runtime. | Uses the npm that came with your Node.js. devEngines can warn or fail on the wrong npm or Node.js version, but npm doesn't switch versions for you. |
| Getting it | A separate install: standalone script, npm, or a container image. pnpm 12 is a native executable that doesn't need Node.js to run. | Already there wherever Node.js is installed. |
| Licence and cost | Open source, MIT. | The CLI is open source, Artistic License 2.0. Both install from the npm registry by default. |
Which to choose
Choose pnpm if
- You run a monorepo where packages depend on each other and CI should build only what a change touched (
pnpm --filter "...[origin/main]"). - You want an import nobody declared to fail on a laptop, not after a dependency upgrade in production.
- Developers and CI runners install many projects, and a warm shared store saves time and disk.
- You want the repo to pin the package manager and Node.js version in one place, without a separate version manager.
Choose npm if
- It's one app, and
npm cifrom a committed lockfile already gives identical installs. - Your tooling assumes a flat
node_modulesor doesn't follow symlinks, and you'd rather not run pnpm withnodeLinker: hoisted. - You want nothing extra to install: contributors, CI images and docs all assume npm.
- You're on npm 12, and its script approval and git and tarball limits cover the supply-chain risks you care about.
Why an undeclared import works under npm and fails under pnpm
npm hoists. If your app depends on express, and express depends on debug, npm usually puts debug at the top of node_modules too, so import debug from "debug" in your own code resolves even though debug isn't in your package.json. It keeps working until an upgrade changes which version gets hoisted, or removes it, and the break shows up in a build that changed nothing in your code.
pnpm puts only what you declared at the top. debug lives in node_modules/.pnpm and is linked only into the folders of the packages that depend on it, so the same import fails on the first install. Teams moving a large codebase from npm to pnpm usually spend most of the migration adding these missing entries to package.json; the site's post on dependency hoisting covers how they surface in monorepos.
node_modules/
├── express/
└── debug/ <- hoisted from expressnode_modules/
├── express -> .pnpm/express@<version>/node_modules/express
└── .pnpm/
├── express@<version>/node_modules/
│ ├── express/
│ └── debug -> ../../debug@<version>/node_modules/debug
└── debug@<version>/node_modules/debug/Installing the same tree in CI
Both tools give you a frozen install; they reach it differently. With npm, use npm ci, not npm install: it refuses to run without a lockfile, fails when the lockfile and package.json disagree, and never writes either file. With pnpm, pnpm install is already frozen when it detects a CI environment and finds a lockfile; passing --frozen-lockfile makes it explicit, and also fails when there is no lockfile at all.
For container builds, pnpm has pnpm fetch, which downloads everything the lockfile lists without reading any package.json. Copy the lockfile and pnpm-workspace.yaml, fetch, then copy the source and run pnpm install --offline: the slow layer stays cached until the lockfile itself changes, even in a monorepo. With npm, the usual pattern is to copy package.json and package-lock.json first and run npm ci before copying the rest.
npm cipnpm install --frozen-lockfileInstall scripts and new releases: what each blocks now
Most compromised npm packages have used postinstall scripts to run code the moment they install, and both tools now answer that the same way: a dependency's install scripts don't run until the project approves that package. pnpm has done this since version 10, and since pnpm 11 an unapproved build script fails the install, so a package that never needed a build can't quietly start running one. npm 12 made scripts opt-in through allowScripts in package.json; by default it skips unapproved scripts and lists them at the end of the install, and strict-allow-scripts turns that into a failure.
Waiting before taking new releases is where the defaults still differ. pnpm 11 and later won't resolve a version until it has been published for a day (minimumReleaseAge: 1440, in minutes), long enough for most malicious releases to be caught and pulled. npm has min-release-age (in days), but you have to set it. Both settings apply when a version is resolved; an install from an existing lockfile already has its versions.
packages:
- "apps/*"
- "packages/*"
catalog:
typescript: ^5.9.0
allowBuilds:
esbuild: true
minimumReleaseAge: 1440npm install-scripts approve esbuild
npm config set strict-allow-scripts=true --location=project
npm config set min-release-age=1 --location=projectMoving a project from npm to pnpm
Run pnpm import in the project: it builds pnpm-lock.yaml from your package-lock.json, preferring the versions already locked, so you start close to what you had. Delete package-lock.json and node_modules, run pnpm install, and pin the version with a packageManager entry in package.json so every machine runs the same pnpm. In a workspace, declare the packages in pnpm-workspace.yaml before importing.
Expect three kinds of follow-up. Imports that only worked because of hoisting now fail: add those packages to package.json. Dependencies that need a build step (native modules, binaries fetched on install) stop at the approval check: run pnpm approve-builds and commit the result. And CI caches change: cache pnpm's store (pnpm store path prints it) instead of ~/.npm. If a tool still can't cope with symlinks, nodeLinker: hoisted gives it a flat tree while keeping the rest of pnpm.
When the package manager isn't the problem
pnpm and npm decide which JavaScript packages land in node_modules. They don't decide which Node.js runs them (beyond pnpm's optional runtime pinning), nor the native libraries and compilers a package like sharp or node-gyp builds against. If installs differ between a Mac and a Linux CI runner despite a frozen lockfile, the drift is usually there: the site's mise vs asdf comparison covers pinning the runtime, and Nix vs Docker covers pinning everything under it.
Frequently asked questions
Is pnpm faster than npm?
Often, especially on repeat installs, because pnpm links files from a store already on the machine instead of copying them, and it works on each package as soon as it's ready rather than in strict stages. How much depends on the project, the cache and the filesystem. Measure your own repo with a cold and a warm cache before deciding on speed alone.
Can pnpm use package-lock.json?
Not directly, but pnpm import creates a pnpm-lock.yaml from it (and from yarn.lock), preferring the versions already locked. After that, pnpm reads only its own lockfile, so delete package-lock.json to avoid two lockfiles drifting apart.
Does pnpm use the same registry as npm?
Yes. pnpm installs from the npm registry by default and reads registry and auth settings from .npmrc, so private registries and tokens carry over. Since pnpm 11, .npmrc holds only auth and registry settings; everything else goes in pnpm-workspace.yaml.
Can npm make node_modules strict like pnpm?
Yes: install-strategy=linked installs packages into node_modules/.store and links them in place without hoisting, so undeclared imports fail. It left experimental status in npm 12. npm still has no shared store across projects, so it doesn't save disk the way pnpm does.
Do I need Corepack to pin pnpm?
No. pnpm reads the packageManager field itself and, by default, downloads and runs the version it names (the pmOnFail setting changes that). Install any recent pnpm once and the project's pin decides which version actually runs.
Why did my install scripts stop running?
On npm 12, dependency install scripts are skipped until allowScripts approves them: run npm install-scripts approve <package>, then npm rebuild. On pnpm, add the package to allowBuilds with pnpm approve-builds.
Sources
Tools change quickly: check versions, defaults and licence terms against each project’s own documentation before you decide.
- pnpm: Motivation
- pnpm: Symlinked node_modules structure
- pnpm: Feature comparison
- pnpm: pnpm install (--frozen-lockfile)
- pnpm: Mitigating supply chain attacks
- pnpm: pnpm import
- pnpm: pnpm fetch
- pnpm: Catalogs
- pnpm v11.0.0 release notes
- npm: config (install-strategy, min-release-age, allow-git)
- npm: npm ci
- npm: npm install-scripts
- npm: Workspaces
- npm CLI changelog (12.0.0)





