# The Lockfile (package-lock.json)

> Discover how lockfiles guarantee deterministic builds by freezing exact versions, integrity hashes, and resolution URLs.

Subject: Package Managers | Lesson 9 of 24 | 3 min read
Interactive lesson: https://codewithanimation.in/package-managers/package-lock-json
Previous: [node_modules Structure & Hoisting](https://codewithanimation.in/package-managers/node-modules-and-hoisting.md) | Next: [Global vs Local Installs](https://codewithanimation.in/package-managers/global-vs-local-install.md)

## The Lockfile Guarantee

To solve the problem of floating SemVer ranges, modern package managers automatically generate a **lockfile** (`package-lock.json` for npm, `yarn.lock` for Yarn, `pnpm-lock.yaml` for pnpm).

The lockfile is an exact, frozen snapshot of your entire dependency tree. It records the exact version of every package installed, down to the deepest sub-dependency.

npm install normally reuses locked versions when they satisfy package.json. If the manifest requires different versions, install can reconcile the tree and update the lockfile. npm ci instead requires agreement and refuses to rewrite the lock.

## Anatomy of a locked dependency

Notice two critical fields inside a lockfile:

1. **`resolved`**: The exact URL where the tarball was downloaded. This skips the step of asking the registry 'where is this package?'.

2. **`integrity`**: A cryptographic hash of the file contents. Before npm extracts the downloaded package, it verifies the hash. If the package was tampered with in transit (or the registry was compromised), the hash won't match, and npm aborts the installation.

## Handling Merge Conflicts

Because lockfiles are large, auto-generated JSON files, they frequently cause Git merge conflicts when multiple developers add dependencies on different branches.

Resolve manifest conflicts and use the package manager to reconcile the lockfile, then review the resulting dependency diff. Do not blindly delete the lockfile or discard another branch's dependency changes.

## Common mistake: Manual Resolution

Never resolve `package-lock.json` conflicts manually. Doing so can corrupt the integrity hashes and break the build for everyone on your team.

## Try it: Lockfile Generator Simulator

Click to resolve dependencies; observe how lockfiles freeze exact versions with `integrity` hashes and `resolved` tarball URLs. Shows merge conflict resolution strategy.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/package-managers/package-lock-json

## Check yourself

### Question 1

What does package-lock.json primarily record?

- A. Resolved dependency-tree details, including exact package versions
- B. Only the version of Node.js installed globally
- C. Every environment variable used in production
- D. The compiled application bundle

Correct answer: A

The manifest can contain ranges while the lockfile captures resolutions and related metadata. This supports reproducible dependency installation.

- Why not B: The lockfile primarily describes package resolutions.
- Why not C: Environment secrets do not belong in a lockfile.
- Why not D: The lockfile is installation metadata, not build output.

### Question 2

The manifest now requires ^2.0.0, but the lockfile still resolves 1.5.0. How do install and ci differ?

- A. Both ignore the manifest and use 1.5.0
- B. install can update the lock; ci reports the mismatch
- C. ci silently upgrades to the newest 3.x release
- D. install always refuses to change a lockfile

Correct answer: B

npm install reconciles incompatible manifest/lock requirements. npm ci requires an existing matching lock and does not rewrite it.

- Why not A: The manifest remains the source of acceptable requirements.
- Why not C: ci neither invents a new range nor repairs the lock.
- Why not D: A conflicting manifest can cause install to update it.

### Question 3

What is a sound team workflow after adding a dependency?

- A. Commit node_modules instead and delete the lockfile
- B. Keep the lockfile only on one developer's computer
- C. Commit the reviewed manifest and generated lockfile together
- D. Treat every lockfile change as automatically safe

Correct answer: C

Review the resolved tree change along with the requirement change. A lockfile helps reproduce dependency choices; it does not guarantee identical complete builds across every environment.

- Why not A: The lockfile tracks resolutions without committing installed directories.
- Why not B: Other environments need the shared resolution information.
- Why not D: Dependency changes still require review and validation.

## Remember this

- Lockfiles record resolved dependency choices; complete build reproducibility also depends on environment and configuration.
- They store `resolved` URLs for speed and `integrity` hashes for security.
- Resolve manifest/lock conflicts deliberately, use the package manager to reconcile them and review the result.
