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

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

问题描述

node_modules 文件夹到底是什么?它的用途是什么?

What exactly is the node_modules folder and what is it for?

我知道当我们使用 npm 下载任何库时,该库会转到文件夹 node_modules.我也知道,当我们要上传它(例如到 GitHub)时,我们必须忽略 node_modules 文件夹,因为它需要很多空间.通过文件 package.json 我们可以使用 npm i 下载所有依赖项.

I know when we download any library with npm, the library goes to folder node_modules. I also know that, when we are going to upload it (to GitHub, for example) we have to ignore the node_modules folder, because it takes a lot of space. Through file package.json we can download all dependencies using npm i.

假设我想将我的应用程序/网站部署到某个服务器/主机,我是否必须将 node_modules 文件夹也上传到服务器?

Let's say I want to deploy my app/website to some server/host, do I have to upload the node_modules folder to server as well?

还有一件事.通常,我会下载我的 jQueryBootstrap 文件,并将内容复制到我项目中的 css/js 文件夹中,但这次我尝试使用 npm 并且一切都转到文件夹 node_modules 而我正在使用 Cordova.当我执行命令 cordova build 时,我的 jQuery 和 Bootstrap 文件都没有生成.

And another thing. Usually, I download my jQuery and Bootstrap files from the website and copy in the content to the css/js folder inside my project, but this time I tried with npm and everything goes to folder node_modules and I'm using Cordova. When I execute the command cordova build, neither my jQuery nor my Bootstrap files are generated.

所以这些是我的问题:

  • 如果我想托管我的项目,我真的必须同时上传 node_modules 文件夹吗?
  • 如果是 Cordova 或 Ionic,我是否也必须将 node_modules 文件夹复制到 www 文件夹?
  • 如果是这样,使用 npm 下载库的意义何在?真的是这样操作的吗?哪一个更好?去官网,下载文件,粘贴到www里面,还是通过npm下载?
  • if I want to host my project, do I really have to upload the node_modules folder as well?
  • And when it's Cordova or Ionic, do I also have to copy the node_modules folder to the www folder?
  • If so, what is the point of using npm to download libraries? Is this how it's really 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 文件夹中,并且 Node.js 会在您导入它们时在那里寻找它们(没有具体路径).我将其称为缓存,因为 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 Node.js 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.jsonpackage-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 isn't any 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天全站免登陆