The node modules directories take up a lot of disc space as we run several tests with UI-based frameworks, and after a while, the disc space starts to run out.
I recently ran out of space on my laptop’s 500GB SSD hard drive and began to investigate what was taking up so much space. On my laptop, I don’t keep any private files or images—at least not more than 1-2 GB worth.
I discovered that my old experiments from years ago were still on my computer and need some cleanup when I examined the code base folder. The simplest method to get rid of unnecessary data without suffering any form of loss was to delete the enormous node modules folder from any project I wasn’t using right away.
To do this, I created the PowerShell script below, which would clear out all of the node modules directories in my code folder.
Get-ChildItem -Path . -Filter node_modules -Recurse | Remove-Item -Force -Recurse
The “Get-Childitem” command in this case retrieves all of the folders with the path “.” and the filter “node module” and feeds them into the “Remove-Item” command. This command deletes all of the files and directories within without asking for confirmation.
I was able to reclaim more than 20GB of storage space after using this programme.
Note that this script will permanently delete files, so make sure to back up your
node_modules directory before running it. It’s also a good idea to exclude any files or directories that your project relies on, such as
.bin directories or configuration files, to avoid accidental deletion.
With this script, you can keep your
node_modules directory clean and reduce the size of your project, making it easier to manage and share with others.