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

查看:53
本文介绍了如何让多个项目共享 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 在你的项目文件夹中没有找到 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

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

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

这样做的一个缺点是你必须手动构建你的 package.json 文件(除非有人知道一种用 grunt 或其他东西自动化的方法).当您安装软件包并将 --save 参数添加到 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天全站免登陆