package-lock.json and dependency cleanup

Understand package-lock.json and use npm ci and npm prune to keep dependency installations clean and consistent.

🎯 Introduction

In this article, you will learn why package-lock.json matters, how npm ci differs from npm install, and how to remove unnecessary dependencies. These skills help prevent problems where a project works locally but fails in production.


🧱 What is package-lock.json?

When you run npm install, NPM downloads packages into node_modules and creates or updates package-lock.json.

This file helps you:

  • Lock the exact version of every dependency, including transitive dependencies.
  • Record download URLs and integrity information, ensuring consistent package contents across installations.
  • Speed up later installations, allowing NPM to reuse the resolved dependency tree from the lockfile.
Think of package-lock.json as a detailed shopping list. package.json says which ingredients to buy; package-lock.json specifies the supplier, quantity, and batch.

⚡ Should you commit package-lock.json to Git?

Yes.

Commit package-lock.json alongside package.json so team members, CI/CD servers, and production environments can install consistent dependency versions.

Do not commit node_modules: it is large and can be recreated from the lockfile. It is usually excluded in .gitignore:

.gitignore
node_modules/

🚀 What is npm ci?

The ci in npm ci stands for Continuous Integration. This installation command is designed for automated environments.

Here is how it compares with npm install:

Featurenpm installnpm ci
Uses package-lock.jsonReads it, but may update itInstalls strictly from the lockfile
Modifies package.jsonMay do soNo
Cleans node_modules before installationNoRemoves it before reinstalling
SpeedSlowerFaster
Recommended useLocal developmentCI/CD and production deployments

In automation scripts, use:

npm ci
If package-lock.json and package.json disagree, npm ci fails rather than updating them. This enforces consistency.

🧹 Remove unnecessary dependencies

npm prune

Sometimes node_modules contains packages that are no longer needed, such as dependencies you installed earlier and later removed from package.json.

npm prune

NPM checks package.json and node_modules, then removes extraneous packages.

Delete node_modules and reinstall

If dependencies behave unexpectedly, for example because of version conflicts or an incomplete installation, try a full reinstall:

rm -rf node_modules package-lock.json
npm install
This clears and reinstalls dependencies, which can resolve many broken-installation problems. Note that it also regenerates package-lock.json from package.json.

🛠 Check dependency security

NPM also provides commands for checking dependency vulnerabilities:

npm audit
npm audit fix
  • npm audit: scan project dependencies for known security vulnerabilities.
  • npm audit fix: try to fix vulnerabilities automatically, usually by upgrading affected packages to secure versions.
npm audit fix may upgrade dependencies. Check that the changes preserve existing functionality; run it on a separate branch and test thoroughly.

❓ Questions and answers

Q1: How do package-lock.json and package.json differ?

package.json describes required dependencies and version ranges. package-lock.json records exact installed package versions and the dependency tree.

Q2: Why does npm ci delete node_modules first?

It ensures a clean installation, preventing old or inconsistent packages from affecting the build.

Q3: Should I commit node_modules to Git?

No. You can reinstall it from package.json and package-lock.json; committing it wastes repository space.

Q4: Is npm audit fix always safe?

No. Dependency upgrades can affect compatibility, so test the project afterward.


🧪 Practice

  1. In your npm-demo project, delete node_modules and package-lock.json, then try:
npm install

and

rm -rf node_modules
npm ci

Compare the two installation processes and their results.

  1. Run npm prune to check for packages left in node_modules after their declarations were removed.
  2. Run npm audit to check for known vulnerabilities in the project's dependencies.

🎉 You now understand NPM lockfiles and dependency cleanup! You can install, manage, publish, and clean up NPM dependencies while keeping team environments consistent. Next, we will explore two popular alternatives: Yarn and pnpm.