Skip to Content

How to Remove npm Packages in NodeJS

At this point, you've probably already set up a Node environment and you've installed numerous packages for additional functionality required outside the box. Equally important, you should know how to uninstall packages you no longer need for your application. Keeping your Node app clean of unnecessary bloat is key to ensuring it runs at optimal speeds.

This tutorial will teach you how to remove npm packages from your local project, global environment, development dependencies, and more.

Method 1: Remove Local Packages

To remove a local npm package, navigate to the root path of your project using a command prompt tool and enter the following command:

cd /path/to/project

Show a list of locally installed packages:

npm list

Identify the package to remove and uninstall it:

npm uninstall [package-name]

The above command will uninstall the chosen package from the project's associated node_modules folder, but will be retained in your project's package.json file, both located in the root directory of your application. This is to ensure locally removed packages can be installed again at a later date.

To remove all dependencies from your node_modules directory and package.json file, run the following command:

npm uninstall --save [package-name]

Method 2: Remove Global Packages

Show a list of globally installed packages:

npm list -g --depth=0

Identify the package to remove and uninstall it by adding either the -g or --global flag to the command:

npm uninstall -g [package-name]
With globally installed npm packages, you can run this command from any path in your command line tool with the same outcome.

Method 3: Remove Development Dependencies

Some packages may be development dependencies, meaning they're only needed for the development phase and not in your production environment. These would be listed as devDependencies in your package.json file, and must be removed by using either the -D or --save-dev flag in the command prompt.

npm uninstall -D [package-name]

Method 4: Remove Unused Packages

Over time, your project might accumulate unused packages. You can clean up your project by removing these packages.

First, install the npm-check package globally:

npm install -g npm-check

Once the package is installed, run npm-check command to display a list of installed packages:

npm-check

Now, use the arrow keys to navigate through each package, and press Space to select individual packages for removal. Once you've made your selection, press the Enter key to remove the selected packages.

Method 5: Prune Packages

The final method is to prune your project directory. This removes packages not listed in dependencies or devDependencies in your package.json file:

npm prune

Verify removal and check for any remaining extraneous packages:

npm list

Conclusion

These methods should cover most scenarios for removing npm packages. Choose the method that best fits your needs, whether you're removing a specific package, cleaning up globally installed packages, or pruning extraneous packages from your

Last Updated: December 04, 2023
Created: August 13, 2022