DEV Community

Keshav Mohta
Keshav Mohta

Posted on

Delet node_modules in Windows using PowerShell

Powershell have many great features, here I am telling how to delete node_modules in windows using powershell

as we know, in Linux/ MacOs we can delete node_modules using rm-rf but this lacks in windows, so create powershell script or just run the command to delete node_modules

  1. navigate to the project root from powershell and run this command
Get-ChildItem -Path $ProjectRootPath -Include "node_modules" -Recurse -Directory | Remove-Item -Recurse -Force
Enter fullscreen mode Exit fullscreen mode

or if you want to make a script which runs from outside of project

then save below script in your powershell profile

to check your profile location type

$Profile

it will give the full path of the profile, normally ends with .ps1

open that profile and paste below code

# delete node modules from a given path
function xnm {
[CmdletBinding()] param (
[Parameter(Mandatory=$true, HelpMessage="Please enter the project path where node_modules are placed", Position=0)]
[String] $ProjectRootPath
)
    if (!($ProjectRootPath | Test-Path)) { 
        Write-Error "Path is not correct. retry" -ErrorAction Stop
    }
    Read-Host -Prompt "Press any key to continue to delete node_modules or CTRL+C to quit"
    Write-Host "Deleting node_modules ..."
    Get-ChildItem -Path $ProjectRootPath -Include "node_modules" -Recurse -Directory | Remove-Item -Recurse -Force
}
Enter fullscreen mode Exit fullscreen mode

now save and restart powershell ( yet to finf how to reload on same session )

now type xnm and it will ask for project root path here just give path name and it will ask to continue and when execution done. it will remove node_modules from your project path.

Hope it helps

Top comments (0)