How To Exclude node_modules Directory When Running collectstatic Command in Django

21 Jun · by Tim Kamanin · 1 min read

If you use npm or yarn to install frontend packages inside your Django project, you may notice, that when you run python manage.py collectstatic command, it ends up collecting huge amounts of files.

That's because by default, collectstatic grabs all content of static directories inside the project, including thousands of files that live inside node_modules directories produced by npm and yarn.

Luckily, it's straightforward to omit node_modules directories altogether; just run your collectstatic command with an additional "ignore" argument, like this:

python manage.py collectstatic -i node_modules

And you're done!

P.S. If you're tired of typing python manage.py everytime you need to run Django command, I recommend you to check a little package of mine called "Django manage.py anywhere": https://github.com/timonweb/Django-manage.py-anywhere, it'll surely save you some keystrokes here and there.

Comments

Required for comment verification



Daisy

Hi, If you omit all node_modules, how do you upload the files that you want to include? For example, I've tested yarn with Ken Wheeler's slick-animation, and all of the files are in node_modules in one of my static directories. Yes, I don't want all of the files in node_modules copied when I run collectstatic, but I need some of them to be! Any advice much appreciated!

Reply · 1 week, 6 days ago