Delete node_modules from SPFx solution (rimraf)

In this SharePoint tutorial, we will discuss how to delete node_modules from SharePoint Framework (SPFx) solutions. We will see how to use the rimraf cmdlet to do this.

Here, we will see different ways to delete the node_modules folder.

By default, when you create an SPFx solution, it adds the node_modules, which is very large. Trying to delete it will take a lot of time.

It will unnecessarily take your space because the folder is very big.

As you can see, when I try to delete, it takes a lot of time to calculate the space.

delete node modules
node_modules

The node_modules will have more than 15,00,000 files, and it will take time to delete them. (I got an approximate count while trying to zip the folder using Winzip.) If you zip the folder, it will be more than 250 MB.

What is rimraf?

In a node-based project, we can use Rimraf to clean up the installed node package files. However, because of the nested subfolder’s length, we can not delete it on a Windows machine, and it will give you an error.

The source file name(s) are larger than the file system supports. Before attempting this operation, try moving to a location with a shorter path name or renaming to a shorter name(s).

So here it is rimraf. This will help us to delete the folder and nested subfolders.

Delete node_modules

First, we need to install rimraf in the Windows machine. For this, run the below command:

Delete node_modules

You can see the install rimraf command below:

how to remove node modules
install rimraf

Once we have installed rimraf, we can run the rimraf command to delete the node_modules folder.

See also  What is Microsoft Fluent UI?

Navigate to the folder and run the below command:

rimraf node_modules

Example:

D:\Project\SPFxProfiles_07_10>rimraf node_modules

Here, the node_modules folder is presented inside the SPFxProfiles_07_10 folder.

delete node_modules

Once you execute the command, the node_modules folder will get deleted.

Delete node_modules from VS Code

Apart from the above method, we can also delete node_modules from visual studio code (vs code).

Open the SPFx solution using Visual Studio Code, right-click on node_modules, and click on Delete, as shown below. It will take some time, but it will delete the folder.

remove node modules

Delete node_modules from winrar

We can also delete the node_modules folder from Winrar. It will take approx less time.

Right-click on the node_modules folder, then click on Add to archive… like below:

rimraf node_modules

Then select Delete files after archiving checkbox option from the Archiving options like below:

delete node modules command

It will create a zip file and then it will delete the node_modules folder.

Later you can delete the zip file, it will be just over 250 MB that will not take more time.

In this tutorial, we learned:

  • What is rimraf
  • How to install rimraf in a windows machine
  • How to delete node_modules using rimraf
  • Delete node_modules from VS Code
  • Delete node_modules from winrar

You may also like the following tutorials:

>