Removing a project’s node_modules folder on Windows is often necessary to fix dependency issues, free up disk space, or start fresh. This guide walks you through the safest methods, including using built-in Windows tools and command-line utilities, to avoid file-locking errors and permission problems. You’ll learn how to delete the folder completely, even when standard Explorer deletion fails.
What You’ll Need
- Administrator access to the computer (for some methods).
- Node.js installed (to use
npxornpmcommands). - Your project folder path (e.g.,
C:\my-project).
Step-by-Step
- Close all related programs. Make sure no editor (VS Code, etc.), terminal, or any process that might be using files inside
node_modulesis open. This includes Node.js servers, build tools, or file watchers. If you’re unsure, close everything and restart your computer to release all file handles. - Open Command Prompt as Administrator. Press Windows, type
cmd, right-click “Command Prompt”, and select “Run as administrator”. Confirm any UAC prompt. - Navigate to your project folder. In the command prompt, type:
cd C:\path\to\your\projectand press Enter. Replace the path with your actual project location. - Use the built-in
rmdircommand. Typermdir /s /q node_modulesand press Enter. The/sflag removes all subdirectories and files,/qruns in quiet mode (no confirmation prompts). This often works when Explorer fails. Wait for the command to finish—it may take a few minutes. - If
rmdirfails, use PowerShell withRemove-Item. Open PowerShell as Administrator (right-click Start > Windows PowerShell (Admin)). Navigate to the project:cd C:\path\to\project. Then run:Remove-Item -Recurse -Force node_modules. The-Forceflag overrides read-only attributes. - If still stuck, use
npx rimraf. In the same PowerShell or Command Prompt, run:npx rimraf node_modules. This uses the popularrimrafpackage (which mimics Unixrm -rf) and handles long paths and permission issues better. It will download the package temporarily if needed. - Verify the folder is gone. Type
dirto list the contents of your project folder. You should not seenode_moduleslisted.
Common Mistakes
- Deleting while a process is using the folder. This is the #1 cause of errors like “file in use”. Always close your editor, terminal, and any Node processes.
- Using File Explorer to delete. Explorer often fails on deep paths or long file names, leaving partial folders behind. Use command-line tools instead.
- Not running as Administrator. Some files have restricted permissions; without admin rights, deletion fails silently or partially.
- Forgetting to reinstall dependencies. After removing
node_modules, you must runnpm install(oryarn) before the project works again.
Tips
- Use a package manager script. Add a script to your
package.json:"clean": "rimraf node_modules"to make future cleanups easier. - Consider using
npm ciinstead ofnpm installafter cleanup. It installs dependencies exactly from the lock file, ensuring a clean, reproducible build. - If you have many projects, use a tool like
npx npkillto find and delete allnode_modulesfolders at once, but be careful—it will ask for confirmation. - Keep your Node.js and npm updated. Newer versions handle long paths better and have fewer permission quirks.
Troubleshooting
If the folder still won’t delete, try these in order:
- Restart Windows in Safe Mode. This prevents most background processes from locking files. Boot into Safe Mode, then run
rmdir /s /q node_modulesfrom an admin Command Prompt. - Check for long path issues. Windows historically limited paths to 260 characters. Enable long paths via Group Policy or registry (search “Enable NTFS long paths” for your Windows version). Then retry deletion.
- Use a third-party unlocker. Tools like “LockHunter” or “Unlocker” can show which process is locking a file and force-close it. Use them cautiously and only from trusted sources.
- If you see “Source file names are larger than is supported by the file system”, rename the parent folder to a shorter name (e.g.,
proj) and try again. - As a last resort, format the drive. Only if the folder is on a secondary drive and you can back up other data. This is extreme and rarely needed.
Frequently Asked Questions
Is it safe to delete node_modules?
Yes. It contains only installed packages and can be recreated with npm install. It’s not part of your source code.
Why does Windows say “The action can’t be completed because the folder is open”?
This means a program (like your editor or a Node process) is using files inside. Close all programs, or restart your computer, then try again.
What’s the difference between rmdir and Remove-Item?
Both delete folders. rmdir is the classic command-line tool; Remove-Item is PowerShell’s cmdlet with more options. They are interchangeable for this purpose.
Do I need to reinstall all packages after deleting node_modules?
Yes. Run npm install or yarn to re-download all dependencies listed in package.json.
Can I delete node_modules while the project is running?
No. Stop any running server or build process first. Otherwise, you’ll get file-lock errors and may corrupt the running app.
Why is node_modules so huge?
It contains thousands of files from all your dependencies, including nested dependencies. It’s normal for it to be hundreds of megabytes or even gigabytes.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Fix Payday 2 Crash Issues on PC: A Step-by-Step Guide
- Remove a Remote Access Trojan from Windows PC
- Google Pixel & Pixel XL: How to Turn Off Snooze
- Facebook as AIM Messenger: Step-by-Step Guide
- iPhone 17 SOS Service Issue: How to Fix It
- How to Remove JEV from Your Browser and Stop It