How to Remove Packages Using the npm uninstall Command. | Warp
Introducing Warp Factories: open, flexible infrastructure for building your own cloud software factory. Learn More
The short answer
To remove locally installed packages, you can use the npm uninstall command from your project’s directory:
Bash
$ npm uninstall <package_name …>
Where:
- package_name is the name(s) of the package(s) you want to remove from the project.
This command will automatically remove the packages located in the node_modules directory and update the dependencies, devDependencies, optionalDependencies, and peerDependencies objects in the package.json file to reflect those changes.
Easily retrieve this command using Warp’s AI Command Suggestions
If you're using Warp as your terminal, you can easily recall npm commands using the Warp AI Command Suggestions feature.
Entering npm uninstall local package in the AI Command Suggestions will prompt an npm uninstall command that can then quickly be inserted into your shell by doing CMD+ENTER.
Removing scoped packages
To remove a scoped package, you can use the following syntax:
Bash
$ npm uninstall @<scope>/<package_name>
Where:
- @scope is the namespace of the user or organization this package belongs to.
For example:
Bash
$ npm uninstall lodash
$ npm uninstall @angular/core
Removing global packages with the -g flag
A global package is a package that's installed system-wide on your machine, eliminating the need for frequent reinstallation.
To remove a global package, you can use the npm uninstall command with the -g flag as follows:
Bash
$ npm uninstall -g <package_name>
$ npm uninstall -g @<scope>/<package_name>
For example:
Bash
$ npm uninstall -g jshint
$ npm uninstall -g @babel/core
Cleaning unused dependencies
After removing packages, your project's node_modules directory may still contain extraneous dependencies that were once required by these packages. Although not explicitly listed as dependencies in the package.json file, they usually remain in your project's file structure and need to be periodically cleaned up in order to reclaim disk space.
To remove these packages and ensure that your project only contains necessary dependencies, you can use the npm prune command as follows:
Bash
$ npm prune
Removing packages without updating the package.json file
When you remove a package, it's important that this change is accurately reflected in the package.json and package-lock.json files. However, there might be instances when you want to uninstall a package without modifying this metadata. These scenarios often involve temporary adjustments, debugging, or environment-specific requirements.
To remove a package without altering the package.json and package-lock.json files, you can use the --no-save flag as follows:
Bash
$ npm uninstall --no-save <package_name>
For example:
Bash
$ npm uninstall --no-save @angular/core
If you want to learn more about cached packages and further optimize your package management, you can read our article on how to clear the npm cache.
Confirming package uninstallation
After running the npm uninstall command, it's essential to verify the successful removal of the package as it helps to maintain the integrity and reliability of your project and reduces potential conflicts between packages.
To get a list of the locally installed packages present in your node_modules folder, you can use the npm ls command from your project's directory as follows:
Bash
$ npm ls
Alternatively, you can use the npm ls command with the -g flag to get a list of the globally installed packages as follows:
Bash
$ npm list -g
If you want to learn more about listing dependencies, you can read our article on how to list locally and globally installed packages with npm.
Unpublishing packages from the registry
Before removing a package from the registry, make sure you're logged in to your npm account. You can log in using the following command and providing your credentials:
Bash
$ npm login
To remove a specific package version from the registry, you can use the npm unpublish command as follows:
Bash
$ npm unpublish <package_name>@<package_version>
For example:
Bash
$ npm unpublish lodash@4.17.21
After unpublishing, you can run npm cache clean to clear the npm cache of references to the removed package:
Bash
$ npm cache clean -f