我已经使用 -g 全局安装了大部分 NPM 模块
但是,我发现每当我使用 Yeoman 创建新的应用程序项目(例如:Angular 项目)时,我都会看到 npm 模块会再次下载并安装在本地 node_modules 文件夹中。
我认为,为每个新项目下载并复制相同的模块是非常浪费的。每个新项目的大小约为 160MB。
有没有办法只下载新项目中全局 npm node_modules 文件夹中尚未提供的模块?比如说,无论如何要自动创建从本地 node_modules 文件夹到全局安装的 npm 模块的符号链接(symbolic link)?
这可能吗?如何设置?
最佳答案
这实际上是设计使然。来自 Node.js blog :
In general, the rule of thumb is:
1.If you’re installing something that you want to use in your program, using require('whatever'), then install it locally, at the root of your project.
2.If you’re installing something that you want to use in your shell, on the command line or something, install it globally, so that its binaries end up in your PATH environment variable.
在本地安装项目依赖项对您来说很重要,因为您无法保证您的计算机上全局安装的内容可以在部署计算机上使用。确实,您可以管理两台机器,或者它们实际上可能是同一台机器,但永远不能保证这种情况。
此外,您的项目依赖于特定版本的 npm 并不罕见,您可能已经在全局范围内安装了更新,从而破坏了需要旧版本的项目。隔离是保持项目正常运行的关键。
如果您确实只需要维护一份副本,则可以使用符号链接(symbolic link)。 npm 链接全局npm
Install it globally, and then npm link coffee-script or npm link express (if you’re on a platform that supports symbolic links.) Then you only need to update the global copy to update all the symlinks as well.
请注意,符号链接(symbolic link)选项实际上仅与手动初始化的项目相关;像 Yeoman 这样的生成器将始终使用本地的,因为它们遵循 Cereal 。
关于angularjs - 如何创建新的应用程序项目而不需要一次又一次下载和安装 npm 模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27251732/