Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Gulp Basics Gulp your JavaScript Workflow Minifying JavaScript Files

Leigh Maher
Leigh Maher
21,830 Points

Why does npm add so many folders to the node_modules folder?

When we install a single plugin e.g. gulp-concat, npm installs a bunch of other folders in the node_modules folder. 163 extra folders in my case. I can't imagine that gulp-concat is dependent on all of these? I'm concerned about bloat and don't want to have unnecessary files and folders in my project. How do we ensure that it only downloads what is necessary?

3 Answers

Kevin Korte
Kevin Korte
28,148 Points

It very well could be. gulp-concat might have a few dependencies, which have a few dependencies, which have a few dependencies.

So while gulp-concat might not have 163 diredt dependencies, if you followed the dependencies of the dependencies, it might be. I'm only speculating here.

And thus, that's the beauty of the node package manager. As developers, we get to completely free ourselves from this dependency hell, and let npm do it's job, which is mange the packages.

Npm is only going to download what is necessary, based on the package.json file in each package. I wouldn't worry about bloat, as that's the beauty of a node_modules folder, no framework should, or if not using a framework, you shouldn't load the entirety of the node_modules folder into an application. The packages should, and likely all do fall around import and exports.

Meaning, packages to do something, export that function as a module. And packages that depend on a dependency, import the module from the dependency. All that to say even though you end up with a lot of folders, you application is only pulling out what it needs, to function.

You shouldn't even be checking the node_modules folder into git. The package.json file is enough to rebuild that folder by another developer, or on another machine.

Don't worry, my most recent project, I have only added about 6 packages myself, but those packages dependencies have added about 110 folders in my node_modules folder, which I truthfully almost never look in.

Well answered, Kevin Korte. :smile: :smile:

Leigh Maher
Leigh Maher
21,830 Points

Thanks Kevin for a very clear answer. Very helpful. Cheers.