npm ci vs npm install
Both commands fill node_modules, but they answer different questions. npm install asks "what should the tree be?" and may change the lockfile to answer it. npm ci (clean install) asks "install exactly what the lockfile says, or fail". Use install while developing, ci in pipelines and Dockerfiles.
npm install |
npm ci |
|
|---|---|---|
| Needs a lockfile | no (creates one) | yes |
Lockfile out of sync with package.json |
updates the lockfile | fails |
Writes package-lock.json / package.json |
yes, when needed | never |
Existing node_modules |
reused, adjusted | deleted first |
Install one package (npm ci lodash) |
yes | no, whole project only |
| Result | the current best tree | exactly the locked tree |
The happy path
mkdir -p svc && cd svc
npm init -y > /dev/null
npm install ms@2.1.3 left-pad@1.3.0 2>&1 | grep added
cp package-lock.json lock.before
touch node_modules/stale-file
npm ci 2>&1 | grep -v deprecated
ls node_modules
cmp package-lock.json lock.before && echo "lockfile untouched"added 2 packages in 603ms
added 2 packages in 248ms
left-pad
ms
lockfile untouched
stale-file is gone: npm ci removed the whole node_modules folder before installing, so nothing left over from an earlier branch or an earlier Node version can leak into the build.
When the lockfile and package.json disagree
Someone added a dependency to package.json by hand (or resolved a merge conflict) and forgot to run npm install. npm install would quietly fix the lockfile. npm ci refuses, which is exactly what you want in CI: the commit is broken, and the build should say so.
mkdir -p svc && cd svc
npm init -y > /dev/null && npm install ms@2.1.3 > /dev/null
npm pkg set dependencies.is-number=^7.0.0
npm ci 2>&1 | sed -n '1,6p'
echo "--- the fix, on a developer machine"
npm install 2>&1 | grep added
npm ci 2>&1 | grep addednpm error code EUSAGE
npm error
npm error `npm ci` can only install packages when your package.json and package-lock.json or npm-shrinkwrap.json are in sync. Please update your lock file with `npm install` before continuing.
npm error
npm error Missing: is-number@7.0.0 from lock file
npm error
--- the fix, on a developer machine
added 1 package in 441ms
added 2 packages in 261ms
npm ci also fails the other way round, when the lockfile lists a package package.json no longer mentions. Either way the message is the same: run npm install, commit the lockfile.
A tampered or corrupted package
The lockfile's integrity hashes are checked on every download. Here the lockfile claims a different hash for lodash than the registry's tarball really has:
mkdir -p svc && cd svc
npm init -y > /dev/null && npm install lodash@4.18.1 > /dev/null
rm -rf node_modules ~/.npm/_cacache
sed -i 's#"integrity": "sha512-[^"]*"#"integrity": "sha512-AAAAbHPp6c0LG2Ad+5yiyZDGaqk0XpQlpQUcJFQr5Wb+Er0TpZq9H2bwNhh+7YuACqnDwwZBK/wxOP5VaOH/vA=="#' package-lock.json
npm ci > ci.log 2>&1; echo "exit $?"
grep -m1 'seems to be corrupted' ci.log | cut -c1-80
grep '^npm error code' ci.logexit 1
npm warn tarball tarball data for lodash@http://localhost:4873/lodash/-/lodash-4
npm error code EINTEGRITY
npm retried, then gave up. In real life this means a broken proxy or mirror, a lockfile edited by hand, or a registry serving different bytes under a version number that should be immutable: all things you want to stop a deployment.
npm ci in pipelines
A typical CI job:
- uses: actions/setup-node@v4
with:
node-version: 24
cache: npm # caches ~/.npm (the download cache), keyed on package-lock.json
- run: npm ci
- run: npm run build
- run: npm test- Cache
~/.npm, notnode_modules.npm cideletesnode_modulesanyway, and a cachednode_modulesbuilt for another OS or Node version is a source of mystery bugs. npm ci --omit=devgives a production-only tree for server images; build first, in an earlier Docker stage.npm ci --ignore-scriptsskips every install script; see Install Scripts & allowScripts.npm install --package-lock-onlyupdates the lockfile without touchingnode_modules: handy for bots that bump versions.
pnpm and Yarn have the same split: pnpm install --frozen-lockfile and yarn install --immutable fail instead of changing the lockfile (both turn it on automatically when they detect CI).
Test yourself
package.json lists a dependency the lockfile doesn't know. What does npm ci do?
- Adds it to the lockfile
- Fails and tells you to run npm install
- Ignores it
- Installs the latest version
What happens to an existing node_modules folder when you run npm ci?
- It is reused
- It is deleted before installing
- It is moved to node_modules.bak
- npm ci fails
What should a CI pipeline cache to speed up npm ci?
- node_modules
- ~/.npm, keyed on package-lock.json
- dist/
- package.json