DEV Community

loading...
Cover image for Delete node_modules like a Pro

Delete node_modules like a Pro

nirazanbasnet profile image Nirazan Basnet Updated on ・1 min read

Usually, while working on multiple projects we complain about the hard drive which is mystically always full.

Whenever we start a new project and run npm install, it downloads a bunch of javascript files in the node_modules folder and we don't care about this after we abandoned the project.

Even after knowing that we can delete it by right-clicking it.

I usually use the bash command which deletes the directories from the current working directory

find . -name 'node_modules' -type d -prune -exec rm -rf '{}' +
Enter fullscreen mode Exit fullscreen mode

But the smartest way to get the job done is to run npx npkill

So, installation through npm is simple. Make sure you have installed node and npm and then run

npm i -g npkill
Enter fullscreen mode Exit fullscreen mode

You can also use it without installing it by running

npx npkill
Enter fullscreen mode Exit fullscreen mode

It easily finds and removes old and heavy node_modules folders

To know more information about npkill, you can visit the site here


I follow FireShip - a youtube channel where we get high quality code tutorials to help you build & ship your app faster, for a long time. So, I came across this video which really helped me.


Conclusion

👏👏 By coming this far I hope you can use this awesome npkill npm package. So, I suggest you give it a try on your project and enjoy it!

Feel free to share your thoughts and opinions and leave me a comment if you have any problems or questions.

Till then,
Keep on Hacking, Cheers

Discussion (23)

pic
Editor guide
Collapse
ianwijma profile image
Ian Wijma

Not sure what is wrong with rm -r node_modules

Collapse
nirazanbasnet profile image
Nirazan Basnet Author • Edited

Nothing is wrong here. With your code, we can delete just within a project, but to delete all the node_modules from the device we can use about code. Thanks!

Collapse
sharpninja profile image
The Sharp Ninja

Better Solution

Get-ChildItem /**/node_modules -Recurse | Remove-Item -Force -Recurse
Enter fullscreen mode Exit fullscreen mode
Thread Thread
itachiuchiha profile image
Itachi Uchiha

I didn't use PowerShell. But it seems a Powershell statement. Right? :P

Thread Thread
sarvasana profile image
Thread Thread
sharpninja profile image
The Sharp Ninja

Yes, PowerShell.

Collapse
harshhhdev profile image
Collapse
nirazanbasnet profile image
Nirazan Basnet Author

Yes !! It really helped me :)

Collapse
mlaraki profile image
mlaraki

You've literally copied eveything, even the title ... The least you can do is aknowledge the real author in your article !

Collapse
qm3ster profile image
Mihail Malo

I recommend github.com/tbillington/kondo instead.
It's fast, is a single tiny executable (thanks, Rust), doesn't come with its own node_modules, and supports not only npm projects.

Collapse
nirazanbasnet profile image
Nirazan Basnet Author • Edited

Didn't know about this. Thanks for sharing I will definitely try this :)

Collapse
matthijsewoud profile image
⚡️ • Edited

Doesn't npm intelligently link already-installed-elsewhere modules? If so, deleting node-modules folders might be detrimental.

Edit: whoops! I was wrong about this one, sorry gang. Maybe I got it confused with Yarn or some other package managers? If anyone knows more about this, that'd be awesome; I feel bad about having loads of copies of the same module at multiple locations on my system.

Collapse
pahund profile image
Patrick Hund

That's the first time I'm hearing this. To my knowledge, every npm project has its own node_modules directory and every package dependency is installed per project. Do you have a link that documents what you are suggesting?

Collapse
tisdadd profile image
Michael Adsit

This was my first time hearing this as well. I know that pnpm is meant to do so.

Collapse
sharpninja profile image
The Sharp Ninja

Better Solution

Get-ChildItem node_modules | Remove-Item -Force -Recurse
Enter fullscreen mode Exit fullscreen mode
Collapse
nirazanbasnet profile image
Nirazan Basnet Author

Didn't know about this, thanks for sharing!

Collapse
john__olson profile image
John Olson

Your last suggestion of npx npkill is the easiest to use. Nothing to install.

Collapse
zakiazfar profile image
zakiAzfar • Edited

Check rimraf it is fast, and it is not limited to node_modules. It is written in JS so can be used in node projects

Collapse
tyanick profile image
Tazoh Yanick

Can we run the command npx npkill from any directory or we must run from the root in order to delete all node_modules files from the device?

Collapse
mariocalin profile image
Mario

Whats is wrong with npm prune?

Collapse
williamgerlach profile image
William Gerlach

Thanks for sharing about Pro.

Collapse
koraylinux profile image
Koray Biçer

If I'm done with the project, i delete from the pc completely. No need to keep in pc since github holding my all works.

I simply call "rm -rf /project"

Some comments have been hidden by the post's author - find out more