package-lock.json and dependency cleanup
🎯 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.
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:
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:
| Feature | npm install | npm ci |
|---|---|---|
| Uses package-lock.json | Reads it, but may update it | Installs strictly from the lockfile |
| Modifies package.json | May do so | No |
| Cleans node_modules before installation | No | Removes it before reinstalling |
| Speed | Slower | Faster |
| Recommended use | Local development | CI/CD and production deployments |
In automation scripts, use:
npm ci
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
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
- In your
npm-demoproject, deletenode_modulesandpackage-lock.json, then try:
npm install
and
rm -rf node_modules
npm ci
Compare the two installation processes and their results.
- Run
npm pruneto check for packages left innode_modulesafter their declarations were removed. - Run
npm auditto 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.
