I’m working on a Node.js project and I think some of my installed packages might be corrupted or outdated. I want to completely remove everything from my node_modules directory and install all the dependencies fresh again.
Is there a quick command or process to wipe out all the current packages and then reinstall them from scratch? I have my package.json file with all the required dependencies listed, so I just need to know the best way to do a clean reinstall of everything.
I’ve heard about deleting the node_modules folder manually, but I’m wondering if there’s a more elegant solution using npm commands directly.
Try npm ci instead of regular npm install after clearing your dependencies. Delete node_modules first, then run npm ci - it does a clean install using your package-lock.json file. This command’s built for automated environments and guarantees you get the exact versions from the lock file, not newer ones. It’s usually faster than regular install and way more predictable. Just make sure your package-lock.json exists and is current before running it, or you’ll have to use the standard install method I mentioned above.
you could also try npm update to refresh packages to their latest versions within your package.json ranges. but if everything’s really broken, then yeah - just delete node_modules like everyone else said.
To refresh all npm packages in your project, the simplest and most effective approach is to delete both the node_modules folder and package-lock.json file, and then execute the npm install command. You can do this by running rm -rf node_modules package-lock.json in your terminal. This action ensures that you remove any potentially corrupted packages and obtain fresh installs of all dependencies listed in your package.json. While there are other npm commands available, this method has proven to be the most consistent for a clean state.