The short answer
To remove locally installed packages, you can use the npm uninstall command from your project’s directory:
$ npm uninstall <package_name …>
Run in Warp
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:
$ npm uninstall @<scope>/<package_name>
Run in Warp
Where:
- @scope is the namespace of the user or organization this package belongs to.
For example:
$ npm uninstall lodash
$ npm uninstall @angular/core
Run in Warp
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:
$ npm uninstall -g <package_name>
$ npm uninstall -g @<scope>/<package_name>
Run in Warp
For example:
$ npm uninstall -g jshint
$ npm uninstall -g @babel/core
Run in Warp
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:
$ npm prune
Run in Warp
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.
For instance, when you're debugging a build issue caused by a specific package and want to remove it temporarily for testing. Or when you're collaborating on a project with other developers and need to keep your adjustments confined to your local environment in order not to corrupt the shared configuration.
To remove a package without altering the package.json and package-lock.json files, you can use the --no-save flag as follows:
$ npm uninstall --no-save <package_name>
Run in Warp
For example:
$ npm uninstall --no-save @angular/core
Run in Warp
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:
$ npm ls
Run in Warp
Alternatively, you can use the npm ls command with the -g flag to get a list of the globally installed packages as follows:
$ npm list -g
Run in Warp
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:
$ npm login
Run in Warp
To remove a specific package version from the registry, you can use the npm unpublish command as follows:
$ npm unpublish <package_name>@<package_version>
Run in Warp
For example:
$ npm unpublish lodash@4.17.21
Run in Warp
After unpublishing, you can run npm cache clean to clear the npm cache of references to the removed package:
$ npm cache clean -f
Run in Warp
Written by
Utsav Poudel
Filed Under
Related Articles
Execute Packages With npx And npm
Discover the power of npm's npx tool, a developer's best friend for running packages without global installs.
Install Dev Dependencies With npm
Learn how to install and update dev dependencies using the npm install command.
Clear npm Cache
Learn how to clear the npm cache on Linux, macOS, and Windows.
How To Update NPM
Learn how to update npm to a specific version using the npm, nvm, or npx commands.
Re-Installing Npm
Learn how to reinstall Node.js and npm on macOS, Linux, and Windows using `curl`, `brew`, `apt`, `nvm`, and Node installer.
How To Reinstall Packages With Npm
Brief guide to reinstalling npm packages using npm
Check Npm Package Version
Check an npm package version within your project
Install NPM Packages From GitHub
Check an npm package version within your project
List Installed Npm Packages
Learn how to list globally and locally installed packages with npm, including their dependencies.