remove node modules from Ubuntu
How To

How to Remove Node Modules in Ubuntu Command Line

Sagar Sharma
Sagar Sharma

Table of Contents

Removing node modules seemed always a challenging task to me (from my college days) as it was always more of a manual process.

But no more!

Recently, I came across a tool npkill that can be used to remove local and global node modules.

But npm modules are stored inside of a normal directory so you can apply the same traditional methods to remove modules too!

In this tutorial, I will walk you through the following:

  • How to use the npkill to remove node modules
  • How to use the rm command to remove node modules

So let's start with the first one.

How to use the npkill command to remove node modules from Ubuntu

The npkill command is an interactive and easy way to remove node modules and this is what I personally use.

For better understanding, this section is divided into two parts:

  • Remove node modules of the local npm packages
  • Remove node modules of the global npm packages

So let's start with the first one.

For the local packages

If you have installed node modules for a specific user, you can use the shown method to remove them.

To use this method, make sure you are inside your home directory and if you don't know how to change the switch to the home directory, use the cd command as shown:

cd ~/

Now, to have an idea of how many modules are installed locally, use the following command:

npm list
List local NPM modules in Ubuntu

Next, use the following command to start the npkill utility:

npx npkill

It will list all the local node modules. You can use the up and down arrow keys to navigate to the modules and hit the SPACEBAR to remove that node:

Use npkill to remove local NPM modules from Ubuntu

Once you're done removing modules, press Ctrl + C to exit from the npkill utility.

For the global packages

To remove global modules, first, you have to find where it is installed.

For that, you can use the following command to list all the globally installed packages and where they are located:

npm list -g
Find the location of global npm modules in Ubuntu Linux

Now, change your directory as per the above command's output. For the maximum number of users, it will be /usr/local/lib:

cd /usr/local/lib

Finally, start the npkill utility and it will list all the global modules:

sudo npx npkill
remove global NPM modules from Ubuntu using the npkill

If you see multiple options, use the up and down arrow keys to navigate through them and hit the SPACEBAR to delete the highlighted option.

How to use the rm command to remove node modules from Ubuntu

This is going to be pretty quick compared to the npkill as you'll be using an in-built command to remove the npm module's directory.

For the global modules:

To remove npm modules for the global packages, first, you need to verify  where they are installed using the following:

npm list -g

It gave me /usr/local/lib directory so what I have to do is use the rm command in the following manner (for most users it with the same):

sudo rm -r /usr/local/lib/node_modules/

Yes, you have to add node_nodules at the end of the line.

For the local modules

Removing the local npm modules with the rm command is even easier.

As the local modules are installed in the home directory, all you have to do is execute the following command:

rm -r ~/node_modules/

Wrapping Up...

This was a quick tutorial on how you can remove node modules with multiple methods.

I hope you removing node modules is no longer a difficult task for you.

Sagar Sharma

Sagar Sharma

A software engineer who loves to tinker with hardware till it gets crashed. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.