I bought a HTML template recently, it contains many plugins placed inside bower_components
directory and a package.js
file inside. I wanted to install another package I liked, but decided to use npm
for this purpose.
When I typed:
npc install pnotify
node_modules
has been created containing about 900 directories with another packages.
What are those? Why did they get installed along with my package? I did some research and turned out that those are needed but really, do I need to deliver my template in production with hundreds of unnecessary packages?
The npm install command is used to install npm packages to a node_modules/ folder. You can also install a specific version of each package by adding the @version keyword after the package names. npm will install the exact versions you defined in the example above.
Solution 1: Ignore the peerDependencies The easiest way to fix the issue is to pass an additional parameter –legacy-peer-deps to npm install. The --legacy-peer-deps tells the npm to ignore the peer dependencies and continue the installation of the package.
By default, npm install will install all modules listed as dependencies in package.
This is a very good question, there are a few things I want to point out.
The V8 engine, Node Modules(dependencies) and require
ing them
Node.JS is built on V8 engine, which it's source code is C++. Which means that Node.JS's dependencies are fundamentally written in C++.
Now, when you're require
ing a dependency, you are taking off code/functions from a c++ program or js library. That's how libraries/dependencies are made.
Libraries have SO many functions that you will not use
For example, take a look at the express-validator module. It has so many functions there. When you require the module, do you use all the functions it provides? The answer is no. People require packages like this just to use one single benefit of it, and all of the functions end up getting downloaded, which takes up unnecessary space.
Think of the node dependencies that are made from other node dependencies as Interpreted Languages
Example:
Javascript is written in C/C++ languages, those languages are written in Assembly. Think of it like a tree. You create new branches each time for more convenient usage and most importantly, to save time . It makes things faster. That is the similar situation for creating new dependencies, when people create a new dependency they use (require
) the ones that already exist, instead of writing a whole C++ program or a JS script because that makes everything easier.
Problem arises when requiring other NPMs for creating a new one
When the authors of the dependencies require other dependencies from here and there just to use a few (small amount) benefits of them, they end up downloading them all, (which they don't really care because they mostly do not worry about the size or they'd rather do this than explicitly writing a new dependency or a C++ addon) and this takes extra space. For example you can see the dependencies that express-validator module uses from this link.
So, when you have big projects that use lots of dependencies you end up taking so much space for them.
Ways to solve this
Number 1
This requires some expert people on Node.JS. To reduce the amount of the downloaded packages, a professional Node.JS developer could go to the directories that modules are saved in, open the javascript files, take a look at their source code, and delete the functions that they will not use without changing the structure of the package.
Number 2 (Most likely not worth your time)
You could also create your own personal dependencies that are written in C++ or more preferably JS, which would literally take up the least space possible depending on the programmer but would take the most time inefficient time to reduce some size instead of doing work. (Note: Most dependencies are written in JS)
Number 3 (Common)
Instead of Using option number 2, you could implement WebPack .
Conclusion && Note
So basically there is no running away from downloading all the node packages, but you could use the solution number 1 if you believe you can do it, which also have the possibility of screwing the whole intention of a dependency.(so make it personal and use it for specific purposes) or just make use of a module like WebPack.
Also, ask this question to yourself, do those packages really cause you a problem?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With