node_modules文件夹的用途是什么? [英] What is the purpose of node_modules folder?

查看:712
本文介绍了node_modules文件夹的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道确切的node_modules文件夹是什么以及它的用途.我知道当我们使用npm下载任何库时,该库将转到node_modules,但是我知道当我们将其上传到github时,我们必须忽略node_modules文件夹,因为它占用大量空间.通过package.json,我们可以使用npm i下载所有依赖项,我的问题是...假设我想将我的应用程序/网站部署到某个服务器/主机,是否还必须将node_modules文件夹也上传到服务器?还有另一件事,通常我是从网站上下载我的jquery和bootstrap并复制到我项目内的css/js文件夹中,但是这一次我尝试使用npm,并且所有内容都进入了node_modules,并且我在使用cordova时执行了cordova命令.构建既不生成我的jquery也不生成我的引导程序.这就是我的问题,如果我想托管我的项目,是否真的也必须上传node_modules?当是cordova或ionic时,我还必须将node_modules复制到www文件夹?如果是,使用npm下载库有什么意义?这是真的吗?哪一个更好?要去网站下载文件并粘贴到www内还是通过npm下载?

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?

推荐答案

node_modules文件夹的用途是什么?

What is the purpose of node_modules folder?

您可以将 node_modules 文件夹视为项目所依赖的外部模块的缓存.当您 npm install 时,它们将从Web上下载并复制到node_modules文件夹中,并且训练了nodejs以便在导入它们时在其中寻找它们(没有特定路径).我之所以将其称为缓存,是因为可以通过重新安装所有相关模块(应在项目文件夹中列出)随时随时从头完全重新创建node_modules文件夹.

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).

但是我知道当我们要将其上传到github时,我们必须忽略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.

这是因为没有理由将所有从属模块的副本存储在自己的github项目中.您所使用的确切版本是已知的,并存储在您的 package.json package-lock.json 中,因此您或使用项目的其他任何人都可以随时下载您的代码然后从其原始来源(包括您使用的完全相同的版本)中提取所有其他相关模块.因此,没有理由在您自己的项目中存储所有这些依赖模块的单独副本.那将是浪费,并使所有这些依赖模块的更新版本复杂化.

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.

这就是我的问题,如果我想托管我的项目,我真的也必须上传node_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.

这篇关于node_modules文件夹的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆