package-lock.json
package.json says what you accept (^4.17.21). package-lock.json says what you got: the exact version of every package in the tree, where it was downloaded from, and a hash of its contents. Commit it. It is the difference between "works on my machine" and "works on every machine, including CI next year".
Anatomy of a lockfile
mkdir -p lock && cd lock
npm init -y > /dev/null
npm install is-odd@3.0.1 is-number@7.0.0 > /dev/null
cat package-lock.json{
"name": "lock",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "lock",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"is-number": "^7.0.0",
"is-odd": "^3.0.1"
}
},
"node_modules/is-number": {
"version": "7.0.0",
"resolved": "http://localhost:4873/is-number/-/is-number-7.0.0.tgz",
"integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==",
"license": "MIT",
"engines": {
"node": ">=0.12.0"
}
},
"node_modules/is-odd": {
"version": "3.0.1",
"resolved": "http://localhost:4873/is-odd/-/is-odd-3.0.1.tgz",
"integrity": "sha512-CQpnWPrDwmP1+SMHXZhtLtJv90yiyVfluGsX5iNCVkrhQtU3TQHsUWPG9wkdk9Lgd5yNpAg9jQEo90CBaXgWMA==",
"license": "MIT",
"dependencies": {
"is-number": "^6.0.0"
},
"engines": {
"node": ">=4"
}
},
"node_modules/is-odd/node_modules/is-number": {
"version": "6.0.0",
"resolved": "http://localhost:4873/is-number/-/is-number-6.0.0.tgz",
"integrity": "sha512-Wu1VHeILBK8KAWJUAiSZQX94GmOE45Rg6/538fKwiloUu21KncEkYGPqob2oSZ5mUT73vLGrHQjKw3KMPwfDzg==",
"license": "MIT",
"engines": {
"node": ">=0.10.0"
}
}
}
}
| Key | Meaning |
|---|---|
lockfileVersion: 3 |
the format written by npm 9+; v1 (npm 6) and v2 (npm 7–8) also exist |
packages[""] |
the root project, a copy of its dependency ranges |
packages["node_modules/x"] |
one entry per folder on disk: the key is the install path, so nesting is explicit |
version |
the exact version chosen |
resolved |
the tarball URL it came from (here the local registry) |
integrity |
a Subresource Integrity hash, sha512- + base64 of the tarball's SHA-512 |
dependencies, engines, license |
copied from the package's manifest so npm can rebuild the tree without fetching manifests again |
Because keys are paths, the lockfile describes the layout, not just versions: node_modules/is-odd/node_modules/is-number is the nested copy you met in the last lesson.
The integrity hash is real
The integrity value is just the SHA-512 of the .tgz file. Download it yourself and compare:
mkdir -p lock && cd lock
npm init -y > /dev/null && npm install ms@2.1.3 > /dev/null
curl -s -o ms.tgz "$(jq -r '.packages["node_modules/ms"].resolved' package-lock.json)"
node -e 'const c = require("crypto"), fs = require("fs");
console.log("sha512-" + c.createHash("sha512").update(fs.readFileSync("ms.tgz")).digest("base64"))'
jq -r '.packages["node_modules/ms"].integrity' package-lock.jsonsha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==
sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==
When the tarball npm downloads doesn't match, the install fails with EINTEGRITY. That protects you against a corrupted cache, a broken mirror, or a registry serving different bytes for a version you already locked.
The lockfile wins over newer versions
With a lockfile present, npm install keeps the locked versions even if the range allows newer ones. npm update is the deliberate way to move forward within the ranges:
mkdir -p lock && cd lock
npm init -y > /dev/null
npm install lodash@4.17.21 > /dev/null
npm pkg get dependencies.lodash
rm -rf node_modules
npm install > /dev/null
npm ls lodash | tail -2
npm update lodash
npm ls lodash | tail -2
npm pkg get dependencies.lodash
jq -r '.packages["node_modules/lodash"].version' package-lock.json"^4.17.21"
└── lodash@4.17.21
changed 1 package in 648ms
└── lodash@4.18.1
"^4.17.21"
4.18.1
The range ^4.17.21 always allowed 4.18.1, but nothing moved until npm update. Notice what changed: the lockfile and node_modules say 4.18.1, while package.json still says ^4.17.21. That's npm's default; npm update --save also raises the range's minimum to ^4.18.1, which is worth doing when your code starts relying on something new in 4.18.
A lockfile, modelled
const registry = { lodash: ['4.17.21', '4.18.1'], ms: ['2.1.3'] };
const highest = (name, major) => registry[name].filter((v) => v.startsWith(major + '.')).at(-1);
// Resolve each ^range to the highest match, unless the lockfile already pins a version.
function install(manifest, lock = {}) {
const next = {};
for (const [name, range] of Object.entries(manifest)) {
const major = range.slice(1).split('.')[0];
next[name] = lock[name] ?? highest(name, major);
}
return next;
}
const manifest = { lodash: '^4.17.21', ms: '^2.1.3' };
// Monday: the registry only has 4.17.21 when you first install.
registry.lodash = ['4.17.21'];
const lock = install(manifest);
console.log('first install :', lock);
// Friday: 4.18.1 is published.
registry.lodash = ['4.17.21', '4.18.1'];
console.log('with lockfile :', install(manifest, lock));
console.log('no lockfile :', install(manifest));first install : { lodash: '4.17.21', ms: '2.1.3' }
with lockfile : { lodash: '4.17.21', ms: '2.1.3' }
no lockfile : { lodash: '4.18.1', ms: '2.1.3' }
That third line is the drift a lockfile prevents: the same package.json installed on two different days gives two different trees.
Lockfile rules of thumb
- Commit it for applications and libraries alike. A library's lockfile isn't used by its consumers, but it makes your own CI reproducible.
- Never edit it by hand. Change
package.jsonor runnpm install x@y/npm update, then commit both files together. - Merge conflicts: resolve
package.jsonfirst, then runnpm install; npm rebuilds the lockfile and resolves the conflict markers itself. - One package manager per repository.
package-lock.json,yarn.lockandpnpm-lock.yamlside by side means three trees that drift apart. ThepackageManagerfield plus Corepack enforces the choice. npm-shrinkwrap.jsonis the same format but is published with a package. Only CLIs that want to pin their whole tree for users should use it.
Test yourself
What is the key of a lockfile entry for a nested package?
- Its name
- name@version
- Its install path, e.g. node_modules/is-odd/node_modules/is-number
- Its integrity hash
The range is ^4.17.21, the lockfile says 4.17.21, and 4.18.1 exists. What does npm install install?
- 4.18.1
- 4.17.21
- It fails
- Whichever is cached
What does the integrity field contain?
- A signature by the author
- A base64 SHA-512 hash of the package tarball
- The git commit of the package
- A checksum of package.json only