Javascript AngularJS 中的 node_modules 目录是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34526844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is node_modules directory in AngularJS?
提问by Dims
I am exploring AngularJS tutorialproject and found it has node_modules
directory inside, which size if 60 megabytes.
我正在探索AngularJS 教程项目,发现它node_modules
里面有目录,大小为 60 兆字节。
Does simple clientside javascript project really need so huge corpus of unknown data?
简单的客户端javascript项目真的需要如此庞大的未知数据语料库吗?
I tried to delete this directory and project still works. I suspect it somehow relates with node.js
and it's npm
but how? Suppose I need to run my project on some conventional web server (not node.js), then how to know, which files/directories are unneeded?
我试图删除这个目录,项目仍然有效。我怀疑它在某种程度上与涉及node.js
和它的npm
,但是怎么样?假设我需要在一些传统的 Web 服务器(不是 node.js)上运行我的项目,那么如何知道哪些文件/目录是不需要的?
Many javascript libraries require to use bower to install them. If I use bower, does this mean I need to keep node_modules
?
许多 javascript 库需要使用 bower 来安装它们。如果我使用凉亭,这是否意味着我需要保留node_modules
?
回答by vvondra
The node_modules
directory is only for build tools.
该node_modules
目录仅用于构建工具。
The package.json
file in the app root defines what libraries will be installed into node_modules
when you run npm install
.
package.json
应用根目录中的文件定义了node_modules
运行npm install
.
Very often with an angular app, on your dev machine or on a build server, you use other Javascript libraries from npm (a node.js package manager) to build your angular app. Tasks can be concatenating resources, using CSS preprocessors like LESS or SASS, minification, replacing of values, etc. etc. The most common tools for managing and running these tasks are called gruntand gulp, which are installed through npm as well.
对于 angular 应用程序,在您的开发机器或构建服务器上,您经常使用来自 npm(node.js 包管理器)的其他 Javascript 库来构建您的 angular 应用程序。任务可以是连接资源、使用 LESS 或 SASS 等 CSS 预处理器、缩小、替换值等。管理和运行这些任务的最常用工具称为grunt和gulp,它们也通过 npm 安装。
When you deploy your app, you only distribute the resulting build, not any of the source files or build tools.
部署应用程序时,您只分发生成的构建,而不分发任何源文件或构建工具。
It is of course possible to write an AngularJS app without building anything.
当然可以在不构建任何东西的情况下编写 AngularJS 应用程序。
edit from comments: When you dive into Angular more, there are more advanced techniques of using libraries installed by npm even in the client app, you then selectively choose the ones you need, not the whole 50MB+ thing. I'd recommend staying with the basic approaches until you get a good grasp on them though.
评论编辑:当您更深入地研究 Angular 时,即使在客户端应用程序中也有使用 npm 安装的库的更高级技术,然后您可以有选择地选择您需要的库,而不是整个 50MB+ 的东西。我建议您继续使用基本方法,直到您很好地掌握它们为止。
回答by Matt Styles
NPM is the node package manager, which installs packages locally into a project, specifically, into the node_modules
folder. From there the package code canbe included into a project, yes, canis the important word there.
NPM 是节点包管理器,它将包本地安装到项目中,特别是安装到node_modules
文件夹中。从那里可以将包代码包含到项目中,是的,可以是那里的重要词。
The browser has no way to include modules in code (yet), so you need to use a library that can expose node's commonJS style modules. Browserify and Webpack are two popular methods of doing so.
浏览器还没有办法在代码中包含模块(目前),所以你需要使用一个可以暴露 node 的 commonJS 样式模块的库。Browserify 和 Webpack 是两种流行的方法。
Angular complicates this by introducing its own module system, which more closely resembles AMD-style modules. There are ways around this, so that you can use node-style modules, maybe your project uses those.
Angular 通过引入它自己的模块系统使这变得复杂,它更类似于 AMD 风格的模块。有很多方法可以解决这个问题,因此您可以使用节点样式的模块,也许您的项目使用了这些模块。
Using npm
for managing dependencies is a great idea, its a fantastic package manager. It is likely in your case though that the project is only built using node
and that the node_modules
folder contains dependencies only related to the build.
使用npm
管理的依赖是一个好主意,它的一个梦幻般的包管理器。在您的情况下,尽管该项目仅使用构建node
,并且该node_modules
文件夹包含仅与构建相关的依赖项,但您的情况很可能。