问题
I would like to know what is exactly node_modules folder and what is for. I know when we download any library with npm, the library goes to node_modules, but I know when we are going to upload it to github we have to ignore the node_modules folder because it takes a lot of space. Through package.json we can download all dependencies using npm i, my question is... Let's say I wanna deploy my app/website to some server/host, Do I have to upload the node_modules folder to server as well? And another thing, usually I download my jquery and bootstrap from the website and copy in css/js folder inside my project, but this time I tried with npm and everything goes to node_modules and I'm using cordova, when I execute the command cordova build neither my jquery nor my bootstrap are generated. So that's my question, if I want to host my project, Do I really have to upload the node_modules as well? and when it's cordova or ionic I also have to copy the node_modules to the www folder? if it is yes, what is the point of using npm to download libraries? is this how really it's done? Which one is better? going to the website download the file and paste inside www or download through npm?
回答1:
What is the purpose of node_modules folder?
You can think of the node_modules
folder like a cache for the external modules that your project depends upon. When you npm install
them, they are downloaded from the web and copied into the node_modules folder and nodejs is trained to look for them there when you import them (without a specific path). I refer to it as a cache because the node_modules folder can be entirely recreated from scratch at any time by just reinstalling all the dependent modules (that should be listed in your project folders).
but I know when we are going to upload it to github we have to ignore the node_modules folder because it takes a lot of space.
This is because there's no reason to store copies of all your dependent modules in your own github project. The exact version you were using is known and stored in your package.json
or package-lock.json
so at any time you or anyone else using your project can download your code and then retch all the other dependent modules from their original source (including even the exact same versions you were using). So, there is no reason to store a separate duplicate copy of all those dependent modules in your own project. That would just be wasteful and would complicate upgrading to a newer version of all those dependent modules.
So that's my question, if I want to host my project, Do I really have to upload the node_modules as well?
If you have your project running on your local machine and you now want to move it to your hosting location, it is best to reinstall all the dependent modules on the hosting machine and NOT copy them from your development machine. This is because the process of installing them on the hosting machine (which might be a different platform or OS than your development machine) may use a bit of a different install process for the specific hosting environment.
来源:https://stackoverflow.com/questions/63294260/what-is-the-purpose-of-node-modules-folder