Join our Newsletter — 33% off our NHI Course
Home› Guides› Lockfiles and Version Pinning for Beginners: What package-lock.json…
Guide Software & AI Supply Chain

Lockfiles and Version Pinning for Beginners: What package-lock.json Is and Why It Protects You

PDF for newsletter subscribers · free to read online

← All guides
By NHI Mgmt Group, under the editorial direction of Lalit Choda Updated 28 November 2026 10 min read
On this page

A lockfile records the exact version of every code package a project uses, so installs match.

This guide is for students, new developers and anyone who has seen a file like package-lock.json and wondered what it is. You do not need to write code to be affected: lockfiles help make sure the software your business runs is the software that was tested.

In short

  • A project's package list often allows a range of versions, so two installs can get different code.
  • A lockfile writes down the exact versions, plus a fingerprint of each package, so every install can match.
  • Pinning means asking for one exact version instead of a range.
  • Save the lockfile with the code, install from it in builds, and read lockfile changes before accepting them.

What you will learn

  • Explain what a lockfile is and why it is saved with the code.
  • Read the ^ and ~ signs in a version range.
  • Spot a lockfile change that needs a closer look.
  • Check that builds install from the lockfile.
Exact version record (lockfile)
a file that notes the exact version of every package a project installed.
Fixing one version (pinning)
asking for one exact version of a package instead of a range.
File fingerprint (hash)
a short code worked out from a file's contents, which changes if the file changes.

What is a lockfile?

Think of a recipe that says "a tin of tomatoes". Two cooks can buy different brands and get different results. Now think of the till receipt from the shop. It says exactly which brand, size and batch was bought.

Code projects have both. The package list, such as package.json, is the recipe. It names the packages the project needs and often allows a range of versions. The lockfile, such as package-lock.json, is the receipt. The npm documentation says it describes the exact set of packages installed, so later installs can produce the same set.[1]

Version pinning is a related idea. It means the package list asks for one exact version, such as 2.4.1, rather than a range. A lockfile pins everything, including the packages your packages need.

Where the comparison stops working: a receipt only records what you bought. A lockfile is also used to buy the same things again, and its fingerprints help check nothing was swapped on the way.

Why does it matter?

Without a lockfile, an install today can pull in a version that did not exist when the code was tested. That new version might have a bug, or in rare cases harmful code from someone who took over the package. The lockfile keeps changes deliberate.

If you are learning to code

What could happen: your project works on your laptop but breaks on a classmate's, because they got newer versions.

Check this: make sure the lockfile is saved with your code.

Next: Software Supply Chain Security for Beginners

If you review other people's code

What could happen: a small code change hides a large lockfile change that adds packages nobody asked for.

Check this: open the lockfile changes and look for new names or new download addresses.

Next: Dependency Intake Review Checklist

If you run builds and releases

What could happen: the build server installs fresh versions, so what ships is not what was tested.

Check this: make sure builds use a command that installs exactly what the lockfile says.

Next: CI/CD Pipeline Security for Beginners

What does it look like?

Here is a made-up package list and the matching lockfile entry for one package.

Example only. All values are made up.

# package.json (the recipe)
"date-helper-EXAMPLE": "^2.4.1"                       (1)
"colour-tools-EXAMPLE": "~1.3.0"                      (2)
"pdf-maker-EXAMPLE": "3.0.2"                          (3)

# package-lock.json (the receipt), one entry
"version": "2.6.0"                                    (4)
"resolved": "https://registry.example.invalid/..."    (5)
"integrity": "sha512-FAKE0000EXAMPLE0000"             (6)
  1. The ^ sign accepts newer versions with the same first number, so 2.6.0 is allowed.
  2. The ~ sign accepts only small bug-fix versions, such as 1.3.5, but not 1.4.0.
  3. No sign means this one version only. It is pinned.
  4. The exact version that was installed.
  5. Where the package was downloaded from.
  6. The fingerprint. In a real file, this line would contain a long code that the install checks the download against.

Why this is risky: without the lockfile, the ^ and ~ ranges let a fresh install pick up newer versions nobody has tested.

The safer way: keep ranges in the recipe if your team prefers, but save the lockfile and install from it. Change versions on purpose, through a reviewed update.

How does it work?

Here is the life of a lockfile in a typical project.

  1. The first install. The package manager reads the package list, picks a version inside each range and writes the exact choices to the lockfile.[1]
  2. The save. The lockfile is saved into the project's shared history with the code. The npm documentation says it is meant to be committed (saved to the shared project).[1]
  3. The repeat install. Other computers install from the lockfile and get the same versions.
  4. The check. Each download is compared with its fingerprint. If the file does not match, the install should stop.[1][4]
  5. The update. When someone updates a package on purpose, the lockfile changes, and that change is reviewed like any other.
Diagram titled Range versus lock. Left panel, labelled Range only: the package list says date-helper caret 2.4.1. Install in March gets 2.4.1, install in June gets 2.5.0, install in September gets 2.6.0, so each install can differ. Right panel, labelled Range plus lockfile: the same package list, plus a lockfile that says 2.4.1 with a fingerprint. Installs in March, June and September all get 2.4.1, and each download is checked against the fingerprint. A green bar underneath says change versions on purpose, through a reviewed update.
Figure 1. A range lets each install drift. A lockfile keeps every install the same until you choose to update.

Build systems often use a stricter install command. The npm documentation says npm ci needs a lockfile and does not change it. If the lockfile and package list disagree, it stops with an error.[2] Python's pip tool has a similar idea, called hash-checking mode. Each package must be pinned to one version with its fingerprint, or the install fails.[4]

A worked example

Reza Whitlock is a junior developer at a small travel booking firm. His first task is to fix a typing mistake on the home page.

He makes the fix and opens a proposed change for review. His teammate, Wren Jovanovic, looks at it. The text fix is one line. But the change also touches the lockfile, with over 400 changed lines.

Wren asks why. Reza is not sure. He had deleted the lockfile on his laptop to fix an install error, and his install made a fresh one. The ranges in the package list let it pick newer versions of dozens of packages.

Wren remembers the recipe and the receipt. The recipe has not changed, but the receipt now lists different brands. None of those versions have been tested with their app.

She suggests Reza restore the lockfile from the shared project and install from it. He does, and his install error turns out to be an old cache on his laptop. Once he clears it, the lockfile install works.

His new proposed change is one line of text and no lockfile change. Wren accepts it.

Afterwards they agree a team rule. Version changes go in their own proposed change, so the lockfile difference is easy to read and review.

Myths and mistakes

What people thinkWhat is true
The lockfile is clutter, so leave it out of the shared project.The npm documentation says it is meant to be committed, so every install matches.[1]
Deleting the lockfile is a quick fix for install errors.A fresh lockfile can pull in many untested versions. Find the real cause instead.
Lockfile changes are too long to read, so skip them.Look for new package names, new download addresses and big version jumps.
Pinning means you never need to update.Pinned versions still get security fixes upstream. Update them on purpose and often.
The ^ sign means "this exact version".It accepts newer versions with the same first number.[3]

What can I do this week?

  • Open one of your projects and check the lockfile is saved with the code.
  • Look at the package list and find which versions use ^, ~ or no sign.
  • Check that your build uses an install command that follows the lockfile exactly.
  • Next time you review a change, open the lockfile part and look for new names.

Checklist

  • Every project has a lockfile saved with its code.
  • Builds install from the lockfile with a strict command, such as npm ci.
  • Version changes go in their own proposed change.
  • Reviewers read lockfile changes for new names, new download addresses and big jumps.
  • Nobody deletes the lockfile to fix an install problem without checking with the team.
  • Pinned and locked versions are still updated regularly, on purpose.

Common questions

What is package-lock.json?

It is npm's lockfile. It records the exact version, download address and fingerprint of every package your JavaScript project installed. Other computers use it to install the same set. npm creates and updates it for you when packages change.[1]

Should I commit my lockfile?

Yes, for most projects. Saving it with the code means teammates and build systems install the same versions you tested. The npm documentation says the file is meant to be committed to the shared project. See Package Managers for Beginners for the bigger picture.[1]

What is the difference between pinning and a lockfile?

Pinning sets one exact version in the package list for the packages you name. A lockfile records exact versions for everything installed, including the packages those packages need. Many teams use ranges in the list and rely on the lockfile, while others pin as well.

What do the ^ and ~ signs mean?

They are version ranges. In npm, ^1.0.4 accepts newer versions with the same first number, such as 1.2.0. The ~ sign, as in ~1.0.4, accepts only small bug-fix versions, such as 1.0.9.[3] With a lockfile, the range only matters when you update.

Does a lockfile stop malicious packages?

It helps, but it is not a full defence. It stops new versions sneaking in unnoticed and checks downloads against fingerprints. It does not judge a package you choose to add or update. Pair it with careful updates and reviews. See Dependency Updates for Beginners.

Where next?

Ready for the detail? Read the practitioner guide: Dependency Pinning vs Version Ranges. It goes further into how each package ecosystem handles locks, when to pin or use ranges, and how to measure the cost of slower updates.

Also useful:

Related NHI Mgmt Group resources: Dependency Intake Review Checklist · Malicious Package and Dependency Confusion Guide · Linux Package Signing and Repository Trust Guide

Key terms

Lockfile
A file recording the exact version of every package a project installed, so later installs match.
Version pinning
Fixing a package to one exact version instead of accepting a range.
Dependency version ranges
Rules in the package list, such as ^ or ~, that say which versions are acceptable.
Semantic versioning
A three-part version number that hints how big a change is: major, minor and patch.
Transitive dependency
A package your project uses indirectly, because another package needs it.
Hash-checking mode
A pip setting that only installs packages whose fingerprints match the ones listed.

Sources and further reading

  1. package-lock.json. npm Docs (npm CLI v10), GitHub. https://docs.npmjs.com/cli/v10/configuring-npm/package-lock-json, accessed 28 Nov 2026.
  2. npm ci. npm Docs (npm CLI v10), GitHub. https://docs.npmjs.com/cli/v10/commands/npm-ci, accessed 28 Nov 2026.
  3. About semantic versioning. npm Docs, GitHub. https://docs.npmjs.com/about-semantic-versioning, accessed 28 Nov 2026.
  4. Secure installs (hash-checking mode). pip documentation, Python Packaging Authority. https://pip.pypa.io/en/stable/topics/secure-installs/, accessed 28 Nov 2026.

PDF for subscribers

Take this guide with you

Every section, table and checklist in one PDF, with links to the sources and related guides. Subscribe to our weekly NHI & AI Identity Journal and we’ll email you a download link.

Free weekly newsletter

Subscribe to the NHI & AI Identity Journal

The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.

Bonus 33% off our NHI Course when you subscribe.

PDF Downloads for Newsletter Subscribers

Get the guide

Enter your details and we’ll email you a download link.

The full guide is free to read online. The PDF is a thank you for subscribing.

Fill in all fields marked * and tick both boxes to continue.