How to Uninstall NPM Packages from a Node.js Project
Removing a dependency from a project is a 2-step process. First, you must delete the dependency from your node_modules/ folder, and second, remove its listing from your package.json. This ensures the package is fully removed. Instead of performing this task manually, we can use the npm uninstall
command.
In this tutorial we’ll:
- Use
npm uninstall
to remove dependencies from a project - Learn how to uninstall global packages
- Use
npm prune
to remove any untracked modules from the node_modules/ directory
By the end of this tutorial, you’ll be able to remove unused or unneeded dependencies from your project.
Goal
Uninstall packages completely from a project, by removing from the node_modules/ folder, and deleting the dependency entry in package.json.
Prerequisites
Watch: Uninstall an NPM package
Uninstall an NPM package
Identify the package to uninstall
You need the name of the specific package to remove. Look in your package.json to find the list of currently installed packages and find the package to uninstall.
Run `npm uninstall <package>`
Run the following, replacing <package>
with the name of the package, or multiple packages seperated by spaces:
npm uninstall <package>
To uninstall a package, you need to remove it from both your node_modules/ folder (where the code actually lives), and package.json (where it is listed as a dependency). Otherwise, the package will either be removed from package.json but still in your node_modules/ folder, or removed from node_modules/ but will be reinstalled next time npm install
is run.
Using the uninstall command will remove a package from node_modules/ and package.json all at once.
After running the command, the cli will uninstall the package and print information to the terminal about how many packages were removed in total. Uninstalling removes the specified package, and all the packages it used internally as dependencies. Even when uninstalling a single package, it is normal to see it remove several others that were used as dependencies only by that package.
Note: Since npm version 5 (released in 2017) uninstalling will update your package.json for you. Previous versions of npm required passing a flag (
--save
) to specify updating your package.json. If you’re on version 5 or above of npm, updating the package.json on when installing or uninstalling is the default behavior.
Check _package.json_ to verify uninstallation
To confirm the package was uninstalled, check your package.json and see that the package is no longer listed as a dependency. If you peek into your node_modules/ folder, the package should be gone as well.
Congrats, you’ve uninstalled a package!
Uninstall global packages
If you’ve installed a package globally, you can uninstall it by passing the the -g
flag when uninstalling:
npm uninstall -g <package-name>
Clean up your node_modules/ folder
Using npm uninstall
helps us cleanly remove packages from our projects. If you use this command, you shouldn’t have any issues removing packages properly. But bugs find a way!
If you have manually deleted some dependency listing from package.json, or for whatever other reason your node_modules/ folder and package.json have gotten out of sync, you can run npm prune
to remove all packages from your node_modules/ folder which aren’t listed in your package.json. This doesn’t update your package.json at all, it just affects what’s installed in node_modules/.
The npm prune
command will clean out any extraneous packages left behind in your node_modules/ folder. You shouldn’t need this command regularly, but if you do want to make sure your node_modules/ only contains the dependencies listed in your package.json, you’ll be glad to know it.
Recap
To uninstall dependencies from your project, use the npm uninstall
command. It will make sure your dependency is completely removed, by deleting it from your node_modules/ folder and removing it from your package.json. Using npm to uninstall packages for you is simpler than manually deleting dependencies from your node_modules/ folder and your package.json. If your package.json and node_modules/ folder get out of sync, from manually deleting dependencies or any other strange reason, you can remove all extraneous packages by running npm prune
.
Further your understanding
- Local dependencies are located in your project’s node_modules/ folder, where do you think global dependencies are stored?
Additional resources
- npm uninstall Documentation (docs.npmjs.com)
- npm Folders Documentaion (docs.npmjs.com)