Safeguard
Developer Tools•

How do I remove an unused dependency safely?

Fewer dependencies mean fewer advisories. A step-by-step way to confirm a package is unused, remove it, prune the tree, and verify nothing broke.

Safeguard Security Team
3 min read

Search for every use, remove it from the manifest, regenerate the lockfile, prune extraneous packages, and run the full test suite. Do it in a small pull request on its own. Removing a dependency shrinks what you must patch and watch, which is one of the cheapest security improvements available to a new developer.

Why removal counts

Every package is code you did not write and must keep current. Each one can carry advisories, install scripts and maintainer risk, plus its own transitive packages. A dependency that nothing uses still sits in your tree and in your scan results. Removing it shrinks all of that at once.

Step by step

  1. Search the code. Look for imports, requires and string references, including config files, build scripts, tests and dynamic loading. A name can appear in a plugin list without an import.
  2. Check tooling. Linters, test runners and bundlers often reference packages by name in config.
  3. Ask the owner. If it is in a shared library or a monorepo package, someone else may depend on it.
  4. Remove it with the package manager's uninstall command, so manifest and lockfile change together.
  5. Prune. The npm documentation says npm prune removes extraneous packages, and with a name argument removes only matching ones. Run it to clear leftovers from your working tree.
  6. Check the lockfile diff. Confirm the removed package and its exclusive transitive packages are gone and nothing unrelated moved.
  7. Test. Run unit tests, a production build and any smoke test.
  8. Ship it alone. A separate pull request makes any regression easy to trace.

Watch for traps

TrapWhat to do
Used only in a scriptSearch scripts and CI config
Peer dependency of another packageCheck peer requirements before removing
Loaded dynamically by nameSearch string references, not just imports
Shared by a sibling packageCheck the whole workspace

Prove the benefit

Compare the dependency count and scan results before and after, and mention them in the pull request. It makes the work visible and encourages more of it. Setting a dependency budget turns this into a habit.

When not to remove

If you are unsure and the package is deep in a framework, leave it and ask. A confident wrong removal is worse than a package that stays another week.

Where Safeguard fits

Safeguard can do the inventory and watching for you. Its SBOM and dependency scanning lists what a repository actually pulls in, reachability analysis helps separate reachable findings from noise, and policies can gate a pull request. You can try a free scan on a repository to see the output.

Try it yourself

Click through it at your own pace, or open it full screen. Look at which findings are in code that is actually called, a useful clue when deciding what to remove first.

Quick check

Does removing a package from package.json delete it from node_modules?

Not always by hand. Use the uninstall command or run npm prune so the tree matches the manifest.

Can a scanner tell me a package is unused?

It can hint, for example through reachability, but confirm with a code search and tests.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.