How to Uninstall an npm Package in Node.js Quickly
To uninstall a package in Node.js using npm, run
npm uninstall package-name in your terminal. This removes the package from node_modules and updates package.json if it was saved as a dependency.Syntax
The basic command to uninstall a package is npm uninstall <package-name>. You can add flags like --save-dev to update your package.json devDependencies accordingly.
npm uninstall package-name: Removes the package fromnode_modulesand updatesdependenciesinpackage.json(default behavior).--save-dev: Removes the package fromdevDependenciesinpackage.json.
bash
npm uninstall package-name npm uninstall package-name --save-dev
Example
This example shows how to uninstall the package lodash from your Node.js project. It removes the package files and updates package.json to no longer list lodash as a dependency.
bash
npm uninstall lodash
Output
<p>removed 1 package and audited 50 packages in 1s
found 0 vulnerabilities</p>
Common Pitfalls
Some common mistakes when uninstalling npm packages include:
- Not running the command in the project folder where
package.jsonis located. - Forgetting to use
--save-devwhen uninstalling dev dependencies, sopackage.jsonis not updated correctly. - Trying to uninstall a package that is not installed, which will show an error.
Always check your current directory and the package name before uninstalling.
bash
npm uninstall lodash npm uninstall lodash --save-dev
Quick Reference
| Command | Description |
|---|---|
| npm uninstall package-name | Remove package and update dependencies |
| npm uninstall package-name --save-dev | Remove package from devDependencies |
| npm uninstall -g package-name | Uninstall global package |
| npm uninstall package-name --no-save | Remove package without changing package.json |
Key Takeaways
Use
npm uninstall package-name to remove a package from your project.Add
--save-dev to remove a package from devDependencies in package.json.Run the uninstall command in the project folder containing package.json.
Check package name spelling to avoid errors during uninstall.
Use
npm uninstall -g package-name to remove global packages.