Logo

How can I uninstall npm modules in Node.js?

Uninstalling an npm module (also called a package) depends on whether you installed it locally (within a particular project’s node_modules folder) or globally (system-wide). Below are the main commands and options.

1. Uninstalling a Local Package

To remove a package from your project’s node_modules and also remove it from your dependencies in package.json, run:

npm uninstall <package-name>

What Happens:

  1. The package is removed from ./node_modules.
  2. The entry is removed from the dependencies field in package.json, if present.

Removing a Dev Dependency

If a package is listed under devDependencies, npm uninstall <package-name> will still remove it from there. Historically, you’d use:

npm uninstall --save-dev <package-name>

But since npm v5, just npm uninstall <package-name> will detect and remove it from the appropriate field automatically.

2. Uninstalling a Global Package

If you installed a package globally (using -g or --global), you must also use the -g flag to remove it globally:

npm uninstall -g <package-name>

Where Are Global Packages Stored?

  • macOS/Linux: Typically in /usr/local/lib/node_modules or in $HOME/.nvm/versions/node/<version>/lib/node_modules if using nvm.
  • Windows: Typically in %USERPROFILE%\AppData\Roaming\npm\node_modules.

When you remove a global package, any global binaries associated with it are also removed.

3. Confirming Removal

After uninstalling, you can check one of the following to ensure it’s gone:

  • Local: Look at your project’s package.json and confirm the package is no longer in dependencies or devDependencies. Also confirm in node_modules it’s removed.
  • Global: Run npm list -g --depth=0 to see your globally installed packages. The uninstalled package should no longer appear.

4. Common Edge Cases

  1. Package Not Found
    • If npm says it “could not find” the package, ensure it’s spelled correctly and verify whether it’s installed locally or globally.
  2. Permissions Errors
    • If you installed Node.js system-wide and npm is complaining about permissions, consider using a Node version manager like nvm or configure npm’s global directory to avoid needing sudo.
  3. Lock Files
    • If using lock files (like package-lock.json), removing the package with npm uninstall will also update the lock file automatically.

Recommended Resource

Summary

  • Local Uninstall: npm uninstall <package-name>
    • Removes from node_modules and from package.json (dependencies or devDependencies).
  • Global Uninstall: npm uninstall -g <package-name>
    • Removes the global package and any associated command-line tools.

With these commands, you can keep your Node.js dependencies clean and organized, removing modules you no longer need from your projects or global environment.

CONTRIBUTOR
TechGrind