0
0
NodejsHow-ToBeginner · 3 min read

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 from node_modules and updates dependencies in package.json (default behavior).
  • --save-dev: Removes the package from devDependencies in package.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.json is located.
  • Forgetting to use --save-dev when uninstalling dev dependencies, so package.json is 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

CommandDescription
npm uninstall package-nameRemove package and update dependencies
npm uninstall package-name --save-devRemove package from devDependencies
npm uninstall -g package-nameUninstall global package
npm uninstall package-name --no-saveRemove 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.