node.js 如何让多个项目共享 node_modules 目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29786887/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:39:53  来源:igfitidea点击:

How can I make multiple projects share node_modules directory?

node.jsnpm

提问by verystrongjoe

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 的情况下,是否可以在多个项目中共享中央 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

回答by tpie

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.

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/node_modules/bar.js /home/ry/node_modules/bar.js /home/node_modules/bar.js /node_modules/bar.js

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

如果在那里找不到它,则它移动到父目录,依此类推,直到到达文件系统的根目录。

例如,如果 '/home/ry/projects/foo.js' 中的文件调用了 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

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 在你的项目文件夹中没有找到 node_modules 目录时,它会自动检查父文件夹。所以让你的目录结构像这样:

-myProjects
--node_modules
--myproject1
---sub-project
--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 installcommand it automatically appends it to the dependencies section or your package.json, which is convenient.

这样做的一个缺点是你必须手动构建你的 package.json 文件(除非有人知道一种用 grunt 或其他东西自动化的方法)。当您安装软件包并将 --save arg 添加到npm install命令时,它会自动将其附加到依赖项部分或您的 package.json,这很方便。

回答by Eymen Elkum

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 上符号链接(符号链接),它就像快捷方式一样工作,但功能更强大。

Simply you need to make a Junctionfor your node_modulesfolder 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.

只需在您想要的任何位置Junction为您的node_modules文件夹制作一个。连接点只不过是原始 node_modules 文件夹的捷径。在您的项目文件夹中创建它,如果使用,将在该文件夹中创建实际的 node_modules npm install

To achieve this you need at least one node_modulesreal 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 上,您可以使用命令提示符,也可以使用应用程序。使用命令提示符给你更多的控制,使用应用程序更容易我建议Link Shell Extension

回答by Benson

Try pnpminstead 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

回答by Chris

By looking at some articles it seems that Lernais a good tool for managing multiple projects inside a single directory (monorepo). It supports modules sharing without duplicating the entire packages in every folder and commands to install them in multiple projects.

通过查看一些文章,Lerna似乎 是在单个目录中管理多个项目的好工具 ( monorepo)。它支持模块共享,无需复制每个文件夹中的整个包,并支持将它们安装在多个项目中的命令。

pnpmis also a simple and efficient tool, which doesn't duplicate those modules which are already installed for other projects.

pnpm也是一个简单而高效的工具,它不会复制那些已经为其他项目安装的模块。

回答by Shahzad Seraj

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

只需打开文件 Project 1/.angular-cli.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_modulesempty folder inside your project directory

并且不要忘记node_modules在项目目录中创建空文件夹

回答by Fabio Guerrazzi

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 它应该包含所有应用程序的所有包。因此,您的应用程序还将共享大部分独特的 package.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),并在您处理它或设置启动脚本以服务任何应用程序时恢复它们