← Back to Node.js, page 2

nodejs-0025 · Medium mid

What does package-lock.json do, and what is the difference between npm install and npm ci?

package-lock.json records the exact version, download location and integrity hash of every package in the dependency tree. npm install can update it, while npm ci installs exactly what the lock file says, deletes node_modules first, and fails if package.json and the lock file disagree. Use npm ci in CI and builds.

Full answer

package.json holds ranges (^4.17.0). Without more information, two installs on different days can pick different versions. package-lock.json fixes that. It lists the whole tree, direct and transitive, with:

  • the exact resolved version,
  • the resolved URL of the tarball,
  • an integrity hash (a SHA-512 checksum) that npm verifies after download,
  • the dependency edges, and flags such as dev or optional.

Commit the lock file for applications. For libraries it is also committed, but consumers ignore it, because they get their own lock file.

npm install reads package.json and the lock file, adds missing packages, may update the lock file when ranges allow, and changes node_modules in place. npm ci (clean install):

  • requires an existing lock file,
  • removes node_modules first,
  • installs exactly the locked tree, and never writes to package.json or the lock file,
  • exits with an error when package.json and the lock file do not match,
  • is usually faster in CI, and gives repeatable builds.

The integrity hash is also a security check: if the tarball content changes for the same version, installation fails. Review lock file changes in pull requests, since a changed resolved URL or integrity value is worth a question.

javascript
// A small sample of a lock file (lockfileVersion 3). Real files are much longer.
const lock = JSON.parse(`{
  "name": "attendance-tracker",
  "lockfileVersion": 3,
  "packages": {
    "": { "name": "attendance-tracker", "dependencies": { "express": "^4.19.2" } },
    "node_modules/express": {
      "version": "4.19.2",
      "resolved": "https://registry.npmjs.org/express/-/express-4.19.2.tgz",
      "integrity": "sha512-sample-hash-1",
      "dependencies": { "accepts": "~1.3.8" }
    },
    "node_modules/accepts": {
      "version": "1.3.8",
      "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.8.tgz",
      "integrity": "sha512-sample-hash-2"
    }
  }
}`);

const root = lock.packages[''];
console.log('range in package.json:', 'express', root.dependencies.express);

for (const [location, info] of Object.entries(lock.packages)) {
  if (!location) continue;
  console.log(
    location.replace('node_modules/', '').padEnd(8),
    'locked at', info.version.padEnd(7),
    '| has integrity hash:', Boolean(info.integrity),
  );
}
console.log('transitive dependency pulled in by express:', Object.keys(lock.packages['node_modules/express'].dependencies));

The lock file text is a made-up sample with fake hashes. The code shows how its structure maps ranges to exact versions. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): range in package.json: express ^4.19.2 express locked at 4.19.2 | has integrity hash: true accepts locked at 1.3.8 | has integrity hash: true transitive dependency pulled in by express: [ 'accepts' ]

Key points

  • The lock file stores exact versions and integrity hashes for the full tree.
  • npm ci installs exactly the lock file and fails on a mismatch.
  • npm install may update the lock file.
  • Use npm ci in CI and Docker builds.

Common mistakes

  • Adding package-lock.json to .gitignore in an application.
  • Running npm install in CI and getting different versions than on the laptop.
  • Editing the lock file by hand.
  • Mixing npm, yarn and pnpm lock files in one project.

Follow-ups

  • How do you update one dependency safely?
  • What does npm audit read?
  • How do overrides in package.json change the lock file?

Tips

  • Contrast the two commands in a table-like sentence: 'install resolves, ci replays'.
  • Mention integrity hashes as a security feature.
  • Say that CI and Docker builds should use npm ci.