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?
每当我做项目时,我都必须下载节点模块的所有依赖项。在不复制node_modules的情况下,是否可以在多个项目*享*节点模块?
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
5 个解决方案
#1
43
You absolutely can share a node_modules directory amongst projects.
您完全可以在项目之间共享node_modules目录。
From node's documentation:
从节点的文档:
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.
如果传递给require()的模块标识符不是本机模块,并且不以'/'开头,.. ./ ',或'。然后节点从当前模块的父目录开始,添加/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.
如果没有找到它,那么它将移动到父目录,等等,直到到达文件系统的根。
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/foo。js' call require('bar.js'), node将按以下顺序查找:
/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 /酒吧。js /home/ry/node_modules /酒吧。js /home/node_modules /酒吧。js / node_modules / bar.js
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:
因此,只需将node_modules文件夹放入项目目录中,并放入所需的任何模块。就像平常一样需要他们。当节点在项目文件夹中没有找到node_modules目录时,它将自动检查父文件夹。让你的目录结构如下:
-myProjects --node_modules --myproject1 ---sub-project --myproject2
-myProjects, node_modules, myproject1 ---子项目,myproject2。
So like this, even your sub-project's dependencies can draw on your main node_modules repository.
就像这样,即使子项目的依赖项也可以在主node_modules存储库中绘制。
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.
这样做的一个缺点是您必须构建您的包。json文件是手动的(除非有人知道如何使用grunt或其他东西自动处理)。当您安装包并将-save arg添加到npm安装命令时,它将自动将其附加到依赖项部分或包。json,这是方便的。
#2
5
I found a trick, just take a look at the Symbolic Links (symlinks) on Windows or Linux, it is working just like shortcuts but more powerful.
我发现了一个诀窍,看看Windows或Linux上的符号链接(symlinks),它就像快捷键一样工作,但功能更强大。
Simply you need to make a Junction
for your node_modules
folder anywhere you want. The junction is nothing but a short cut to your original node_modules folder. Create it inside your project folder where the actual node_modules would have been created if used npm install
.
只需为node_modules文件夹创建一个连接即可。连接只是对原始node_modules文件夹的一个捷径。在您的项目文件夹中创建它,如果使用npm安装,实际的node_modules将被创建。
To achieve this you need at least one node_modules
real folder then make a Junction to it in the other projects.
要实现这一点,您需要至少一个node_modules真实文件夹,然后在其他项目中对其进行连接。
On Windows, you can either use the Command Prompt, or use an application. Using the Command Prompt gives you a bit more control, using an application is easier I suggest Link Shell Extension.
在Windows上,您可以使用命令提示符,也可以使用应用程序。使用命令提示符可以使您获得更多的控制,使用应用程序更容易,我建议链接Shell扩展。
#3
1
Main directory should look like this
主目录应该是这样的
node_modules
Project 1
Project 2
Project 3
Project 4
just open the file Project 1/.angular-cli.json
只需打开文件项目1/.angular .json
change the schema
改变模式
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
to
来
"$schema": "./../node_modules/@angular/cli/lib/config/schema.json"
and don't forget to create node_modules
empty folder inside your project directory
不要忘记在项目目录中创建node_modules空文件夹
#4
0
Let's assume that having a single node_modules it should contain all the packages for all applications. thus your apps will also share most of the unique package.json entries (just the name should change)
让我们假设只有一个node_modules,它应该包含所有应用程序的所有包。因此,你的应用程序也将分享大多数独特的软件包。json条目(只需更改名称)
my idea would be to have a single root and multiple src level as below
我的想法是将一个根级和多个src级如下所示
root\package.json
root\node_modules
root\\..
root\app1\src\\..
root\app2\src\\..
the only issue you might face would be having a backup of json (or tsconfig) for any app and restore them when you work on it or setup your startup scripts to serve any app
您可能面临的唯一问题是为任何应用程序提供json(或tsconfig)备份,并在处理时恢复它们,或者设置启动脚本以服务于任何应用程序
#5
0
Try pnpm instead of npm.
尝试使用pnpm而不是npm。
pnpm uses hard links and symlinks to save one version of a module only ever once on a disk.
pnpm使用硬链接和符号链接在磁盘上只保存一个模块的一个版本。
Install with:
安装:
npm install -g pnpm
To update your existing installations (and sub-directories) use:
更新现有安装(及子目录)的用途:
pnpm recursive install
#1
43
You absolutely can share a node_modules directory amongst projects.
您完全可以在项目之间共享node_modules目录。
From node's documentation:
从节点的文档:
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.
如果传递给require()的模块标识符不是本机模块,并且不以'/'开头,.. ./ ',或'。然后节点从当前模块的父目录开始,添加/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.
如果没有找到它,那么它将移动到父目录,等等,直到到达文件系统的根。
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/foo。js' call require('bar.js'), node将按以下顺序查找:
/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 /酒吧。js /home/ry/node_modules /酒吧。js /home/node_modules /酒吧。js / node_modules / bar.js
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:
因此,只需将node_modules文件夹放入项目目录中,并放入所需的任何模块。就像平常一样需要他们。当节点在项目文件夹中没有找到node_modules目录时,它将自动检查父文件夹。让你的目录结构如下:
-myProjects --node_modules --myproject1 ---sub-project --myproject2
-myProjects, node_modules, myproject1 ---子项目,myproject2。
So like this, even your sub-project's dependencies can draw on your main node_modules repository.
就像这样,即使子项目的依赖项也可以在主node_modules存储库中绘制。
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.
这样做的一个缺点是您必须构建您的包。json文件是手动的(除非有人知道如何使用grunt或其他东西自动处理)。当您安装包并将-save arg添加到npm安装命令时,它将自动将其附加到依赖项部分或包。json,这是方便的。
#2
5
I found a trick, just take a look at the Symbolic Links (symlinks) on Windows or Linux, it is working just like shortcuts but more powerful.
我发现了一个诀窍,看看Windows或Linux上的符号链接(symlinks),它就像快捷键一样工作,但功能更强大。
Simply you need to make a Junction
for your node_modules
folder anywhere you want. The junction is nothing but a short cut to your original node_modules folder. Create it inside your project folder where the actual node_modules would have been created if used npm install
.
只需为node_modules文件夹创建一个连接即可。连接只是对原始node_modules文件夹的一个捷径。在您的项目文件夹中创建它,如果使用npm安装,实际的node_modules将被创建。
To achieve this you need at least one node_modules
real folder then make a Junction to it in the other projects.
要实现这一点,您需要至少一个node_modules真实文件夹,然后在其他项目中对其进行连接。
On Windows, you can either use the Command Prompt, or use an application. Using the Command Prompt gives you a bit more control, using an application is easier I suggest Link Shell Extension.
在Windows上,您可以使用命令提示符,也可以使用应用程序。使用命令提示符可以使您获得更多的控制,使用应用程序更容易,我建议链接Shell扩展。
#3
1
Main directory should look like this
主目录应该是这样的
node_modules
Project 1
Project 2
Project 3
Project 4
just open the file Project 1/.angular-cli.json
只需打开文件项目1/.angular .json
change the schema
改变模式
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
to
来
"$schema": "./../node_modules/@angular/cli/lib/config/schema.json"
and don't forget to create node_modules
empty folder inside your project directory
不要忘记在项目目录中创建node_modules空文件夹
#4
0
Let's assume that having a single node_modules it should contain all the packages for all applications. thus your apps will also share most of the unique package.json entries (just the name should change)
让我们假设只有一个node_modules,它应该包含所有应用程序的所有包。因此,你的应用程序也将分享大多数独特的软件包。json条目(只需更改名称)
my idea would be to have a single root and multiple src level as below
我的想法是将一个根级和多个src级如下所示
root\package.json
root\node_modules
root\\..
root\app1\src\\..
root\app2\src\\..
the only issue you might face would be having a backup of json (or tsconfig) for any app and restore them when you work on it or setup your startup scripts to serve any app
您可能面临的唯一问题是为任何应用程序提供json(或tsconfig)备份,并在处理时恢复它们,或者设置启动脚本以服务于任何应用程序
#5
0
Try pnpm instead of npm.
尝试使用pnpm而不是npm。
pnpm uses hard links and symlinks to save one version of a module only ever once on a disk.
pnpm使用硬链接和符号链接在磁盘上只保存一个模块的一个版本。
Install with:
安装:
npm install -g pnpm
To update your existing installations (and sub-directories) use:
更新现有安装(及子目录)的用途:
pnpm recursive install