npm uninstall: How to Remove (and Update) npm Packages

By Marko Denic
Contents
  1. Uninstall a package
  2. Several packages at once
  3. Dev dependencies
  4. Global packages
  5. Cleaning up
  6. Remove everything and reinstall
  7. Find unused dependencies
  8. Check what's installed
  9. Updating packages
  10. See what's outdated
  11. Update all packages
  12. Update to the latest major versions
  13. yarn and pnpm equivalents
  14. FAQ

To remove a package from your project, run npm uninstall with its name.

Uninstall a package

This deletes the package from node_modules, removes it from dependencies or devDependencies in package.json, and updates package-lock.json. Commit both files afterwards.

bash
npm uninstall lodash

# Short aliases, all identical
npm remove lodash
npm rm lodash
npm un lodash

Several packages at once

bash
npm uninstall lodash moment axios

Dev dependencies

npm uninstall finds the package whether it’s in dependencies or devDependencies, so you don’t need a flag. -D (--save-dev) still works if you prefer being explicit.

bash
npm uninstall -D eslint

Global packages

Packages installed with -g live outside your project, so remove them with -g too. List what’s installed globally first.

bash
npm ls -g --depth=0
npm uninstall -g typescript

Cleaning up

Remove everything and reinstall

When node_modules is in a strange state (errors after switching branches, a broken install), run npm ci. It deletes node_modules and does a clean install that matches package-lock.json exactly.

bash
npm ci

Find unused dependencies

Over time, projects collect packages nobody imports anymore. Knip scans your code and lists unused dependencies, files and exports.

bash
npx knip

Check what's installed

npm ls shows the installed version of a package and why it’s there. Handy when a package you removed is still in node_modules: something else depends on it.

bash
npm ls                 # top-level packages
npm ls lodash          # which packages pull in lodash

Updating packages

See what's outdated

npm outdated lists every package with a newer version. Wanted is the newest version your package.json range allows; Latest is the version under the latest tag, usually the newest release.

bash
npm outdated

Update all packages

npm update updates everything to the newest version allowed by the ranges in package.json (for ^1.2.0, the newest 1.x). It never jumps to a new major version, which is where breaking changes live.

bash
npm update                # all packages, within their ranges
npm update lodash         # just one

Update to the latest major versions

To move past the ranges, install a package @latest, or use npm-check-updates to bump every range in package.json. Read the changelogs and run your tests: major versions can break your code. Commit first, so you can roll back with git.

bash
npm install react@latest

# All packages to their latest versions
npx npm-check-updates -u
npm install

yarn and pnpm equivalents

Task npm yarn pnpm
Remove a package npm uninstall x yarn remove x pnpm remove x
Remove a global package npm uninstall -g x yarn global remove x (v1) pnpm remove -g x
Update within ranges npm update yarn upgrade (v1) pnpm update
Update to latest npx npm-check-updates -u yarn upgrade --latest (v1) pnpm update --latest
Clean install npm ci yarn install --frozen-lockfile (v1) pnpm install --frozen-lockfile

Stick to one package manager per project: mixing them creates two lock files that disagree.

FAQ

Does npm uninstall remove the package from package.json?

Yes. Since npm 5 it removes the entry from package.json and package-lock.json automatically. Add --no-save to remove it from node_modules only.

Why is the package still in node_modules after uninstalling?

Another package depends on it. Run npm ls package-name to see which one.

How do I uninstall all npm packages?

Delete the node_modules folder. To also drop them from the project, remove the entries from package.json and run npm install to update the lock file.

What’s the difference between npm install and npm ci?

npm install can update the lock file. npm ci deletes node_modules and installs exactly what the lock file says, failing if it doesn’t match package.json. Use npm ci in CI and deployments. Pin the Node.js version there too: here’s how to check and update your Node version.