如何使多个项目共享node_modules目录? [英] How can I make multiple projects share node_modules directory?

查看:1638
本文介绍了如何使多个项目共享node_modules目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我创建项目时,都必须下载节点模块的所有依赖项.无需复制node_modules,是否仍然可以在多个项目中共享中央node_modules?

Whenever I make projects, I have to download all dependencies of node modules. Without copying the node_modules, Is there anyway to share the central node_modules in multiple projects?

类似于以下内容,我每次必须运行许多命令.

like the followings, I have to run many commands every time..

npm install gulp-usemin                                                                        
npm install gulp-wrap
npm install gulp-connect
npm install gulp-watch
npm install gulp-minify-css
npm install gulp-uglify
npm install gulp-concat
npm install gulp-less
npm install gulp-rename
npm install gulp-minify-html

推荐答案

您绝对可以在项目之间共享一个node_modules目录.

You absolutely can share a node_modules directory amongst projects.

来自 节点的文档 :

From node's documentation:

如果传递给require()的模块标识符不是本机模块, 并且不以'/'、'../'或'./'开头,则节点从 当前模块的父目录,并添加/node_modules,以及 尝试从该位置加载模块.

If the module identifier passed to require() is not a native module, and does not begin with '/', '../', or './', then node starts at the parent directory of the current module, and adds /node_modules, and attempts to load the module from that location.

如果在此处找不到它,那么它将移至父目录,然后 以此类推,直到到达文件系统的根目录为止.

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.

例如,如果位于"/home/ry/projects/foo.js"的文件名为 require('bar.js'),则节点将在以下位置查找 此命令:

For example, if the file at '/home/ry/projects/foo.js' called require('bar.js'), then node would look in the following locations, in this order:

/home/ry/projects/node_modules/bar.js/home/ry/node_modules/bar.js /home/node_modules/bar.js/node_modules/bar.js

/home/ry/projects/node_modules/bar.js /home/ry/node_modules/bar.js /home/node_modules/bar.js /node_modules/bar.js

因此,只需将一个node_modules文件夹放入您的项目目录中,然后放入所需的任何模块.像平常一样要求他们.当节点在您的项目文件夹中找不到node_modules目录时,它将自动检查父文件夹.因此,使目录结构像这样:

So just put a node_modules folder inside your projects directory and put in whatever modules you want. Just require them like normal. When node doesn't find a node_modules directory in your project folder, it will check the parent folder automatically. So make your directory structure like this:

-myProjects --node_modules --myproject1 ---sub-project --myproject2

-myProjects --node_modules --myproject1 ---sub-project --myproject2

这样,即使子项目的依赖项也可以在主node_modules存储库中使用.

So like this, even your sub-project's dependencies can draw on your main node_modules repository.

以这种方式进行操作的一个缺点是,您将必须手动构建package.json文件(除非有人知道使用grunt或其他方式自动执行此操作的方法).当您安装软件包并将--save arg添加到npm install命令时,它将自动将其附加到依赖项部分或package.json,这很方便.

One drawback to doing it this way is you will have to build out your package.json file manually (unless someone knows a way to automate this with grunt or something). When you install your packages and add the --save arg to an npm install command it automatically appends it to the dependencies section or your package.json, which is convenient.

这篇关于如何使多个项目共享node_modules目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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