npm uninstall command

This tutorial provides guidance on uninstalling packages from npm. In Node.js projects, when a package becomes unnecessary, you can use the npm uninstall command to remove it.

This command serves to uninstall packages both locally and globally. When executed, it not only deletes the package from the node_modules folder within the project but also removes it from various dependency sections, including dependencies, devDependencies, peerDependencies, and optionalDependencies from packages json files.

npm uninstall [options] package-name

This command uninstalls the specified package, removing it from the node_modules folder. Here are the available options:

  • -g: Allows uninstallation on a global location.
  • -s or --save: This option allows the removal of package dependencies from package.json, package-lock.json, and npm-shrinkwrap.json.
  • --no-save: Use this option to exclude the package from removal in package.json, package-lock.json, and npm-shrinkwrap.json.
  • -d or --save-dev: Enables the removal of devDependencies from package.json, package-lock.json, and npm-shrinkwrap.json.
  • --no-save-dev: Use this option to exclude the devDependencies from removal in package.json, package-lock.json, and npm-shrinkwrap.json.
npm uninstall --save package-name

It does following

  • Removes the package from dependencies section in package-lock.json and package.json files
  • Uninstall the package from node_modules

This uninstall the package-name and removes from the node_modules folder

npm uninstall --save-dev package-name

It does following

  • Removes the package from devDependencies section in package-lock.json and package.json files
  • Uninstall the package from node_modules

Uninstall global package

The following steps required to unsintall global package.

First, Find out installed packages with global scope using below command

npm ls -g

or

npm list -g

ls is an alias for list. -g is a globally uninstall package

Here is an example

E:\>npm ls -g
C:\Users\Kiran\AppData\Roaming\npm
+-- @angular/[email protected]
+-- @lhci/[email protected]
+-- @nestjs/[email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
`-- [email protected]

To uninstall astro from the global scope

npm uninstall -g astro

This removes the package from global scope.

It removes the astro from global npm location node_modules folder.

You can find the global location path using npm root -g

c:\>npm root -g
C:\Users\user\AppData\Roaming\npm\node_modules

Package Uninstallation verify.

To ensure the package has been successfully removed and the uninstall command executed, follow these steps in the terminal:

  • Check the contents of the node_modules folder:
    • On Mac, Linux, or Unix, use the command: ls -l
    • On Windows, use the command: dir
  • Verify if the package folder has been removed from the node_modules directory.
  • If you used options such as -s or -d, check the corresponding dependencies sections in your package.json file to confirm if the package has been removed.