package-lock.json is the file npm writes to record the exact version, download URL and integrity hash of every package in your node_modules, including dependencies of dependencies. Commit it, so that npm ci on any other machine installs the identical tree instead of whatever newer versions your package.json ranges allow today.
Why package.json alone isn't enough
package.json lists version ranges. When you run npm install [email protected], npm saves "ms": "^2.1.3". Per npm's semantic versioning docs, a caret range accepts minor and patch updates: ^2.1.3 allows 2.1.4 or 2.9.0, but not 3.0.0. A tilde range such as ~2.1.3 allows patch updates only.
So two people running npm install a month apart can get different code from the same package.json. That applies to every package in the tree, and most projects have far more indirect dependencies than direct ones. The lockfile pins all of them.
What's inside package-lock.json
We created an empty project with npm 9.8.1 (Node 18) and ran npm install [email protected]. This is the complete lockfile it wrote:
{
"name": "lockdemo",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "lockdemo",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"ms": "^2.1.3"
}
},
"node_modules/ms": {
"version": "2.1.3",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
"integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA=="
}
}
}
"packages"maps each folder path innode_modulesto what is installed there. The empty key""is your project itself.versionis the exact version installed, not a range.resolvedis where the tarball came from.integrityis a hash of the tarball (sha512 here). If a download doesn't match it, npm refuses to install it.
Other fields appear when they apply: dev and optional mark devDependencies and optional dependencies, hasInstallScript flags packages with install scripts, and link marks a symlinked local package.
How nested dependencies show up
Next we added [email protected], which itself depends on ms at exactly 2.1.2. The lockfile now has four entries (summarised; the real file also has resolved and integrity for each):
"" 1.0.0 {"debug":"^4.3.4","ms":"^2.1.3"}
"node_modules/debug" 4.3.4 {"ms":"2.1.2"}
"node_modules/debug/node_modules/ms" 2.1.2 {}
"node_modules/ms" 2.1.3 {}
Your project uses ms 2.1.3, and debug gets its own private copy of ms 2.1.2 nested under its folder. npm ls --all shows the same tree:
[email protected] C:\work\lockdemo
+-- [email protected]
| `-- [email protected]
`-- [email protected]
The lockfile records this exact layout, so every install reproduces it rather than working it out again.
lockfileVersion 1, 2 and 3
| lockfileVersion | Written by | Notes |
|---|---|---|
| (none) | before npm 5 | Very old |
| 1 | npm 5 and 6 | Uses the "dependencies" section, now called legacy data |
| 2 | npm 7 and 8 | Has "packages" plus the legacy section, kept up to date for npm 6 |
| 3 | npm 9 and later | Backwards compatible to npm 7; the npm 9 file above has no legacy section |
If a teammate on an older npm sees the whole file rewritten after an install, a version mismatch like this is usually why. Agree on one npm major version for the project.
Should you commit package-lock.json?
Yes. npm's documentation says the lockfile "is intended to be committed into source repositories". That is how CI, deployments and teammates get the same tree. It also turns dependency changes into a reviewable diff: an upgrade that pulls in a new indirect package shows up in the pull request.
One thing the lockfile does not do: it is never published to the npm registry. If you publish a library, the people who install it resolve your dependency ranges themselves. Only npm-shrinkwrap.json, which has the same format, travels with a published package.
npm ci vs npm install
npm install | npm ci | |
|---|---|---|
| Needs a lockfile | No (creates one) | Yes, fails without one |
| Writes package.json or the lockfile | Yes, when they change | Never |
| Lockfile and package.json disagree | Updates the lockfile | Exits with an error |
| Existing node_modules | Updated in place | Deleted first |
| Add a single package | Yes | No, whole project only |
Use npm install while developing and npm ci in CI and deploy scripts. To see the mismatch error, we changed "debug": "^4.3.4" to "^3.2.7" in package.json by hand and ran npm ci. npm 9.8.1 printed:
npm ERR! code EUSAGE
npm ERR!
npm ERR! `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 ERR!
npm ERR! Invalid: lock file's [email protected] does not satisfy [email protected]
The fix is what the message says: run npm install, check the diff, and commit both files together.
How to fix a merge conflict in package-lock.json
Don't hand-edit the conflict markers in the lockfile. Since npm 5.7.0, npm can merge it for you:
- Resolve any conflicts in
package.jsonby hand. - Run
npm install --package-lock-only. npm reads the conflicted lockfile, writes a merged one that contains the dependencies from both branches, and leavesnode_modulesalone because of--package-lock-only. - Run your tests, then commit.
Deleting the lockfile and reinstalling also "fixes" a conflict, but it re-resolves every range at once and can upgrade dozens of indirect packages you never meant to touch.
Common questions while working with it
Why did package-lock.json change when I only ran npm install?
npm updates the lockfile whenever it changes node_modules or package.json. Typical reasons: someone edited package.json without reinstalling, a different npm major version upgraded the lockfileVersion, or a range now resolves to a newer release because the lockfile entry was missing.
How do I update one package without touching the rest?
Run npm install name@version (or npm update name to move within the current range). Only that package and whatever it needs change in the lockfile. npm outdated lists what's behind.
Python has the same problem with a different file; see pip install -r requirements.txt for how pinning works there.
Frequently asked questions
- Should I add package-lock.json to .gitignore?
- No. npm's documentation says the file is intended to be committed to source repositories. Committing it is what makes teammates, CI and deployments install the same dependency versions. Ignoring it throws that guarantee away.
- Is it safe to delete package-lock.json and run npm install?
- It works, but it is rarely what you want. npm resolves every range in package.json again, so many indirect dependencies can jump to newer versions in one go. To fix a broken or conflicted lockfile, resolve package.json first and run npm install so npm repairs the lockfile in place.
- What is the difference between package-lock.json and npm-shrinkwrap.json?
- They have the same format. package-lock.json cannot be published to the npm registry, while npm-shrinkwrap.json can, so it pins dependencies for people who install your package. If both exist, npm-shrinkwrap.json wins. npm recommends shrinkwrap only for CLI tools and similar deployed packages.
- What is node_modules/.package-lock.json?
- It is a hidden lockfile npm v7 and later write inside node_modules so they can skip rescanning the folder. It always uses lockfileVersion 3 and is ignored when another tool changes node_modules. You don't commit it, and if you edit files in node_modules by hand you should delete it.
- Do yarn and pnpm use package-lock.json?
- No. Yarn writes yarn.lock and pnpm writes pnpm-lock.yaml. Pick one package manager per project and commit only its lockfile, because two lockfiles can drift apart.