Lock file
Example
echo "package-lock.json" >> .gitignore
git add package.jsonnpm install zod
git add package.json package-lock.json
git commit -m "feat: add zod"Committing the lock file with package.json makes every install resolve the same exact versions.
Common mistakes
- Deleting the lock file to fix an install error and silently upgrading dozens of packages.
- Adding
package-lock.jsonto.gitignore, so every machine installs different versions.
Key points
- The lock file is committed and is not listed in
.gitignore. - Exactly one lock file exists, matching the package manager the project uses.
- CI installs strictly from the lock file with
npm cior a frozen-lockfile flag. - Lock file conflicts are fixed by rerunning the install, not by deleting the file.
Interview questions
How do I resolve merge conflicts in package-lock.json?
Resolve the conflicts in package.json first, then run npm install. npm can merge a conflicted lock file and rewrites it in a clean state.
Should libraries commit a lock file?
Yes, for reproducible development and CI. The lock file is not published, so users still resolve versions from your package.json ranges.
What is the difference between package-lock.json and yarn.lock?
They do the same job for different tools. npm reads package-lock.json, Yarn reads yarn.lock, pnpm reads pnpm-lock.yaml.