AI summary
How package-lock.jsonThe file npm writes next to package.json with the exact version of every installed package. The article calls it the lockfile. pins your dependency versions, why it belongs in the repo, and how npmThe package manager for JavaScript. It downloads the packages a project uses from the internet. install and npm ciAn npm command that installs exactly what package-lock.json says, and stops with an error if it doesn't match package.json. treat it differently.
- package.jsonThe file at the root of a JavaScript project that lists its dependencies and which versions each one may use. says which versions each package may use. package-lock.jsonThe file npm writes next to package.json with the exact version of every installed package. The article calls it the lockfile. records the exact version that got installed.
- Commit package-lock.jsonThe file npm writes next to package.json with the exact version of every installed package. The article calls it the lockfile. and use npm ciAn npm command that installs exactly what package-lock.json says, and stops with an error if it doesn't match package.json. in CIContinuous integration: a service that builds and tests your code automatically every time you push. and deployments.
- npmThe package manager for JavaScript. It downloads the packages a project uses from the internet. install can rewrite package-lock.jsonThe file npm writes next to package.json with the exact version of every installed package. The article calls it the lockfile.. npm ciAn npm command that installs exactly what package-lock.json says, and stops with an error if it doesn't match package.json. never does: it stops with an error instead.
If your package.json says "express": "^4.18.0", that isn’t a version. It’s a range of acceptable versions. Say you install the project today and a teammate installs it two months later. If a new 4.x came out in between, you’re running different code. The same goes for the packages ExpressA popular JavaScript library for building web servers. The article uses it as an example package. itself depends on.
package-lock.json, the lockfile, closes that gap. npmThe package manager for JavaScript. It downloads the packages a project uses from the internet. installs packages into the node_modules folder, and it writes the lockfile whenever it changes that folder or package.json.
The lockfile records the exact version of every installed package, including the packages your packages depend on, and where each one was downloaded from.
It also stores an integrity hash for each package. That’s a fingerprint of the package file, so npmThe package manager for JavaScript. It downloads the packages a project uses from the internet. can check that a new download is the same file it locked.
There’s a speed benefit too: npmThe package manager for JavaScript. It downloads the packages a project uses from the internet. can skip asking the registryThe online database npm downloads packages from, at registry.npmjs.org. again about packages it has already installed.
Commit the lockfile
The npmThe package manager for JavaScript. It downloads the packages a project uses from the internet. docs say the file is meant to go in your repository. Dependency changes then show up in diffs, so when something “worked yesterday” you can see which package moved.
If you publish a package, the lockfile doesn’t ship with it. Your users’ npmThe package manager for JavaScript. It downloads the packages a project uses from the internet. picks versions from your ranges on its own, so the file only affects your project.
What ^ and ~ allow
Versions follow major.minor.patch: a bug fix bumps patch, a backward-compatible feature bumps minor, and a breaking change bumps major. The symbol at the start of a range, the caret (^) or the tilde (~), says which digits can move.
npm install vs npm ci
npm install is the everyday command. npm ci is the one meant for CIContinuous integration: a service that builds and tests your code automatically every time you push. and deployments: it installs exactly what the lockfile says.
The two commands treat the lockfile differently:
Here’s the same thing with real packages. I ran it with npmThe package manager for JavaScript. It downloads the packages a project uses from the internet. 12.2.2 on Node.jsThe program that runs JavaScript on a server instead of in a browser. 24.
The --no-audit and --no-fund flags only turn off npmThe package manager for JavaScript. It downloads the packages a project uses from the internet.’s security and funding notes. head keeps the first lines of the error. npm ls express shows the installed ExpressA popular JavaScript library for building web servers. The article uses it as an example package. version and where it sits in the dependency tree.
The newest 4.x on the registryThe online database npm downloads packages from, at registry.npmjs.org. was 4.22.3, but npm install kept the locked 4.18.2:
# package.json: "express": "^4.18.0". Lockfile: express 4.18.2.
$ npm install --no-audit --no-fund
added 68 packages in 346ms
$ npm ls express
[email protected] /app
`-- [email protected]
# Change the range in package.json to "^5.0.0", then:
$ npm ci 2>&1 | head -6
npm error code EUSAGE
npm error
npm error `npm ci` can only install packages when your package.json and package-lock.json are in sync. Please update your lock file with `npm install` before continuing.
npm error
npm error Invalid: lock file's [email protected] does not satisfy [email protected]
npm error Invalid: lock file's [email protected] does not satisfy [email protected]
$ npm install --no-audit --no-fund
added 4 packages, removed 7 packages, and changed 28 packages in 1s
$ npm ls express
[email protected] /app
`-- [email protected]The real error goes on with a line for every package that doesn’t match the lockfile, then npm ci’s usage help. Nothing gets installed. npm install rewrites the lockfile and moves to 5.x.
Use npm ci in CIContinuous integration: a service that builds and tests your code automatically every time you push. and deployments. It fails if the lockfile is missing or doesn’t match package.json.
The two files can stop matching without anyone noticing. For example, someone changes a range in package.json by hand and commits it without running npm install. The lockfile still holds the old version. If the new range doesn’t accept that version, npm ci stops before anything is installed, as in the output above.
A lockfile conflictTwo changes edited the same lines of a file in different ways, so Git can't pick one. Someone has to decide what stays. resolved badly during a merge can leave the same mismatch. So after any merge that touched the lockfile, run npm ci: it shows the mismatch right away. I cover this case in my article on git merge –squash.
npm ci also deletes node_modules before it starts, and it never writes to package.json or the lockfile.
My site’s deploy works this way. Its GitHub ActionsGitHub's built-in service that runs scripts, such as a build or a deploy, when you push code. workflow runs npm ci before the build and the checks. The rest of that setup is in my Terraform and Cloudflare Pages article.
While developing, use npm install to add packages and npm update to move within your ranges. By default npm update leaves package.json alone; it updates node_modules and the lockfile.
Don’t edit the lockfile by hand. Deleting it and regenerating it isn’t a safe fix either: npmThe package manager for JavaScript. It downloads the packages a project uses from the internet. picks a version for every range again, so you end up doing a bulk upgrade without meaning to.
