Lock file

Term side of the Card.
Topic: Package Managers
Unit title: Lock file
Categories: File, Concept
Explanation side of the Card

Explanation: The file generated by a package manager, such as `package-lock.json`, that records the exact versions of all dependencies and their sub-dependencies. Committed to the repository, it makes every environment install identical versions and speeds up installs.

Example

Wrongbash
echo "package-lock.json" >> .gitignore
git add package.json
Rightbash
npm 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.json to .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 ci or 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.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy