Loading

Quipoin Menu

Learn • Practice • Grow

node-js / package-lock.json Explained
tutorial

package-lock.json Explained

You've probably noticed a file called `package-lock.json` appearing in your projects after you install packages. What is it? Why is it important? Let's demystify this file.

What is package-lock.json?

`package-lock.json` is automatically generated whenever you modify `node_modules` or `package.json`. It describes the **exact dependency tree** that was installed – including all sub-dependencies and their exact versions.

Think of package-lock.json as a snapshot of your `node_modules` folder. It records exactly what was installed, so that anyone else (or your CI server) can install the exact same dependencies.

Why Do We Need It?

Without `package-lock.json`, if two developers run `npm install` at different times, they might get slightly different versions of packages (due to version ranges like `^`). This can lead to the classic "it works on my machine" problem. `package-lock.json` ensures that every install is identical.

What's Inside package-lock.json?

It's a large JSON file that includes:
  • The exact version of every package (including nested dependencies).
  • The integrity hash to verify the package hasn't been tampered with.
  • The resolved URL where the package was downloaded from.
  • Dependencies of each package (the full tree).

Here's a simplified snippet:
{
  "name": "my-project",
  "version": "1.0.0",
  "lockfileVersion": 2,
  "packages": {
    "node_modules/express": {
      "version": "4.18.2",
      "resolved": "https://registry.npmjs.org/express/-/express-4.18.2.tgz",
      "integrity": "sha512-5/PsL6iGPdfQ/lKM1UuielYgv3BUoJfz1aUwU9vHZ+J7gyvwdQXFEBIEIaxeGf0GIcreATNyBExtalisDbuMqQ==",
      "dependencies": {
        "accepts": "~1.3.8",
        ...
      }
    }
  },
  "dependencies": { ... } <!-- legacy format for older npm -->
}

Should You Commit package-lock.json?

YES, absolutely! You should always commit `package-lock.json` to your version control system (like git). This ensures that everyone working on the project, and your deployment servers, use the exact same dependency versions.

When Does package-lock.json Update?

  • When you run `npm install` and it changes `node_modules` (e.g., adding a new package).
  • When you run `npm update` to update packages within ranges.
  • When you manually edit `package.json` and run `npm install`.

package-lock.json vs package.json

package.jsonpackage-lock.json
Lists dependencies with version ranges.Lists exact versions of every package (including nested).
Human-edited.Auto-generated, shouldn't be edited manually.
Defines what versions are acceptable.Locks down exactly what was installed.

Troubleshooting with package-lock.json

If you ever run into weird dependency issues, you can try deleting `node_modules` and `package-lock.json` and then running `npm install` again. This will regenerate everything from scratch.
rm -rf node_modules package-lock.json
npm install

Two Minute Drill

  • `package-lock.json` locks the exact versions of every dependency (including nested ones).
  • It ensures reproducible builds – everyone gets the same dependencies.
  • Always commit it to your repository.
  • It's auto-generated – don't edit it manually.
  • If you have issues, try deleting it and `node_modules` and reinstalling.

Need more clarification?

Drop us an email at career@quipoinfotech.com