我可以为所有 angular2/4 个项目使用一个 node_module 文件夹吗

Can I use one node_module folder for all angular2/4 projects

我正在学习 Angular,每次创建一个新项目大约需要 5 分钟,因为 CLI 创建了 100MB 文件夹 "node_modules"。并且这个文件夹中的文件总是相同的(除非你添加了一些我从不添加的依赖项)。有没有办法为每个项目使用一个 node_modules 文件夹?

只有通过节点 (npm install) 安装的包才能位于 node_modules 文件夹中。这是因为,如果有人想要安装您的项目,而不是下载包含 node_modules 的整个项目。他们输入 npm install。

基于 packag.json,node_modules 现在将下载到 node_modules 文件夹。

所以你可以把angular放在node_modules文件夹里如果它是一个npm包。不可以,您不能将自己的文件放入此文件夹。

因此,您只需将 package.json 复制到每个项目,然后 运行 npm install。那么所有的node_modules都一样。

看看https://yarnpkg.com/blog/2017/08/02/introducing-workspaces/

Yarn Workspaces is a feature that allows users to install dependencies from multiple package.json files in subfolders of a single root package.json file, all in one go.

Making Workspaces native to Yarn enables faster, lighter installation by preventing package duplication across Workspaces. Yarn can also create symlinks between Workspaces that depend on each other, and will ensure the consistency and correctness of all directories.

npm install -g yarn

您可以全局安装所有依赖项或创建从一个地方到每个项目的符号链接。

但这是不好的做法,正确的方法是为每个项目使用单独的 node_modules,即使您使用相同的包也是如此。一旦你需要在不同的项目中使用同一个包的不同版本并且常见 node_modules 会引起很多头痛。

如果你只是想更快地安装包而不关心版本是否匹配,请尝试使用npm cache and npm install --prefer-offline。我没有使用它,但相信它应该有用。