How to remove node_modules folder

Learn how to remove the entire folder or just specific packages from your node_modules folder

Posted on April 23, 2021

The node_modules folder is used to save all downloaded packages from NPM in your computer for the JavaScript project that you have. Developers are always recommended to do a fresh install with npm install each time they downloaded a JavaScript project into their computer.

Still, there might be cases when you want to remove the folder from your computer. Such as when copying the project to a hard drive for backup and removing unused packages. This tutorial will show you how to remove certain NPM packages and remove the whole node_modules folder from your local computer.

First, let’s see how to remove the node_modules folder entirely.

How to remove node_modules folder

To delete the node_modules folder from your JavaScript project, you can use the following command for Mac / Linux OS:

rm -rf node_modules

The command above will first delete the content of node_modules recursively until all of it is deleted, then it will remove the node_modules folder too.

If you have multiple node_modules folders in many different projects, then you need to use the following command to find the folders and delete them all.

Go to the parent folder of all your node_modules folders and run the following command:

find . -name "node_modules" -type d -prune | xargs du -chs

It will find all node_modules located inside the folder and all its subfolders. Here’s an example output from my computer:

$ find . -name "node_modules" -type d -prune | xargs du -chs
130M	./server-components-demo/node_modules
244M	./single-spa-basic/node_modules
244M	./react-boilerplate/node_modules

As you can see from the output above, I have three node_modules folders inside three different subfolders. To delete them all, I need to use the following command:

find . -name "node_modules" -type d -prune -exec rm -rf '{}' +

The command will find all node_modules directory inside the folder and its subfolders and execute rm -rf command on the selected files.

For Windows, removing node_modules package may cause an error saying the source file names are larger than what is supported by the file system:

The source file name(s) are larger than is supported by the file system. 
Try moving to a location which has a shorter path name, 
or try renaming to shorter name(s) before attempting this operation

When you see this error, you need to remove the node_modules folder by using the rimraf NPM package.

If you’re using NPM version 5.2.0 or above, then you can use NPM package runner called npx to run rimraf without installing as follows:

npx rimraf node_modules

If your NPM version is lower than 5.2.0, then you need to install the package globally using NPM as follows:

npm install -g rimraf

Then remove the node_modules folder with the following command:

rimraf node_modules

If you have many node_modules folders, then you can go to the parent folder that contains all the node_modules folder and execute rimraf with the following pattern:

rimraf ./**/node_modules

The double asterisk ** pattern will make rimraf finds all node_modules folder recursively and delete them all.

Deleting specific packages from node_modules folder

At times, you may want to remove specific NPM packages from your node_modules folder. You can do so by using the npm uninstall command as shown below:

npm uninstall <package name>

Or you can also remove the package name manually from package.json file and run another npm install command. The npm install command will check your node_modules folder and remove packages that are not listed as a dependency in package.json file.

Now you know how to remove node_modules folder and removing specific packages from it 😉

Related articles:

Level up your programming skills

I'm sending out an email once every Wednesday with the latest programming tutorials. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.