node_modules位于开发阶段

时间:2017-01-13 08:50:10

标签: angular nexus node-modules

最近我开始使用Angular 2 app开发,我遇到了一些问题。我有多个项目使用某些节点模块,我在项目文件夹中安装了npm install这些模块。但后来我开始思考:如果我有很多项目并为每个项目安装节点模块,它需要相当多的磁盘空间,并且有多个节点模块安装多次(在多个项目中)。所以我试图找到一种方法在一些公共文件夹中安装节点模块,以使该文件夹成为我的项目使用的某种库。但是我想到了另一个问题:如果不同的项目需要不同版本的同一模块怎么办?如何在同一文件夹中安装多个模块版本?

也许我的想法完全是错误的,所以我要求你解决我的问题。在开发阶段是否有任何处理节点模块的常用方法?每个项目都有自己的节点模块文件夹,还是有一些常见的文件夹?什么是最常见的方法(如果有的话)?

谢谢!

编辑:在Nexus Repository docs中找到此页面 - 它表示代理npm注册表是“减少重复下载”,我正在尝试这样做。我已经设法创建了一些代理npm存储库,并在.npmrc文件中指定了我的代理URL。当我运行npm install时,资产和组件被添加到我的代理存储库中,但这并没有解决我的问题 - 节点模块也安装在我的项目文件夹中。我不太明白如何使用Nexus Repository“减少重复下载”。任何人都可以向我解释如何实现这一目标吗?

1 个答案:

答案 0 :(得分:2)

通常,没有什么可以阻止您在项目中共享node_modules。来自节点documentation

If the module identifier passed to require() is not a core module, 
and does not begin with '/', '../', or './', then Node.js starts at 
the parent directory of the current module, and adds /node_modules, 
and attempts to load the module from that location. Node will not 
append node_modules to a path already ending in node_modules.
If it is not found there, then it moves to the parent directory, 
and so on, until the root of the file system is reached.

因此,无论何时提交包裹,都要写出正确的路径。