How to remove unnecessary packages from Node.js project using npm

I’m working on a Node.js project and I’ve noticed that my node_modules folder has gotten pretty messy over time. There are packages installed that I don’t actually need anymore because I removed them from my package.json file during development.

Is there an npm command that can automatically clean up these orphaned dependencies? I want to remove any packages that are sitting in node_modules but aren’t listed in my package.json anymore.

Whenever I update my app, I’d love to have these leftover modules deleted automatically so my project stays clean. What’s the best way to handle this cleanup process?

honestly, just nuke the whole node_modules folder and run npm install again. i do this when npm prune misses stuff or when things get weird. takes a few minutes but you’ll have a completely clean setup that matches your package.json exactly.

You can also run npm ci then rm -rf node_modules && npm install for a complete fresh start. This rebuilds everything from scratch using your package-lock.json file. I’ve found it super helpful when packages seem corrupted or there’s weird caching beyond just orphaned dependencies. Takes longer since it reinstalls everything, but sometimes that’s what you need. Another option is npm install --production if you want to remove dev dependencies specifically, though that might not fix your issue. I usually stick with npm prune for regular cleanup but go nuclear with the full reinstall when things get really messy.

Manual cleanup sucks. I was dealing with this across multiple microservices and it ate up hours every week.

I automated the whole thing instead. Built a workflow that finds orphaned packages and cleans them during deployment.

Write a script that runs npm prune, logs what gets removed, and emails you a summary. Throw it in your CI/CD pipeline and forget about it.

Mine uses automation tools - now every push triggers cleanup automatically. No more remembering commands or dealing with massive node_modules folders.

You can extend it for other maintenance stuff too. Outdated package checks, security audits, whatever.

For easy setup, check out https://latenode.com

Running npm prune will fix this. It removes any packages from node_modules that aren’t listed in your package.json. I use it all the time and it’s perfect for cleaning up leftover modules. Just run it from your project root and it’ll automatically find and delete orphaned packages. One heads up though - make sure your package.json is current before running it, since that’s what npm uses to decide what stays. I learned that one the hard way. Now I run it regularly, especially after removing dependencies during dev work.

Pro tip: npm prune --production keeps only production dependencies and removes all dev packages. I use this when prepping builds for production since dev dependencies just bloat deployment packages. Regular npm prune handles orphaned packages, but this flag targets devDependencies even when they’re still in package.json. With good package.json maintenance, it keeps dev and production environments way cleaner. Just don’t run it in your local dev setup unless you want to reinstall all your dev tools.