如何自动链接本地 npm 包? [英] How can I automatically link local npm package?

查看:39
本文介绍了如何自动链接本地 npm 包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建两个相互依赖的私有 npm 包.

I'm buidling two private npm packages that depends on each other.

说我有:

project
  /my-commons
     package.json :
     {
       name : "my-commons",
       version : "0.0.1"
       ...
     }

  /my-server
     package.json :
     {
       dependencies : {
          "my-commons" : "0.0.1"
       }
     }

我可以使用npm link"来安装commons"包.所以谁愿意开始在服务器上工作:

I can use 'npm link' to install the 'commons' package. So anyone willing to start working on server has to do :

  • 结帐项目
  • cd my-server
  • npm 链接 ../my-commons
  • npm 安装

在/my-server/node_modules 中添加了指向 ../my-commons 的符号链接,一切都很好.

And a symlink to ../my-commons is added in /my-server/node_modules, and everything's fine.

但是有没有办法告诉 npm 'my-commons' 包总是在那个文件夹中,这样你就可以这样做:

Is there however a way to tell npm that 'my-commons' package will always be in that folder out there, so that you could just do :

  • 结帐项目
  • cd my-server
  • npm 安装

还是我在这里遗漏了一些明显的东西?

Or am I missing something obvious here ?

谢谢

推荐答案

也许吧.

但首先:如果 my-server 需要 my-commons 那么最好将它保存在 my-server/node_modules 中 即使那是多余的.在大多数情况下,最好将模块的依赖项与应用程序的其余部分隔离.

But first: If my-commons is needed by my-server then it is most likely a good idea to keep it in my-server/node_modules even if that is redundant. In most cases it's best keep a module's dependencies isolated from the rest of your application.

在这种情况下,npm link 可以在开发过程中使用,当您正在处理 my-commons 并希望使用 my-server 而不必 npm 发布 my-commons.

In that scenario, npm link can be used during development, when you're working on my-commons and want to use the changes in my-server without having to npm publish my-commons.

在生产环境中,您不会希望使用 npm link,因为依赖模块将无法控制它们最终使用的链接模块的版本.如果 my-server 依赖于 my-commons 0.1.0,但是你 npm link 编辑了你的 1.0.1-pre-release 版本my-commons 模块可能会崩溃.

In production you will not want to use npm link, because dependent modules will lose control over which version of the linked module they end up with. If my-server depends on my-commons 0.1.0, but you npm linked your 1.0.1-pre-release version of the my-commons module all hell might break loose.

但是,从 1.2.10 版(随节点 0.8.19 一起提供)NPM 支持对等依赖项.对等依赖项允许您指定 my-server 要求安装除了"my-server 之外的 my-commons.这并不使您能够在 my-serverrequire("my-commons") 但如果 my-servermy-commons 的插件.更多信息:http://blog.nodejs.org/2013/02/07/peer-dependencies/

However, since version 1.2.10 (shipping with node 0.8.19) NPM supports peer dependencies. Peer dependencies allow you to specify that my-server requires that my-commons be installed "besides" my-server. This does not enable you to require("my-commons") inside my-server but could be useful if my-server is a plugin for my-commons. For more information: http://blog.nodejs.org/2013/02/07/peer-dependencies/

最后,既然你说你正在开发私有包:如果安装包给你带来了麻烦,因为你不能将你的包发布到公共 NPM 注册表,有查看指定依赖项的替代方法(git-、http-URL、TGZ 文件):https://docs.npmjs.com/files/package.json#dependencies

And finally, since you said that you are developing private packages: If installing the packages is what is causing trouble for you, because you can't publish your packages to the public NPM registry, have a look at alternative ways to specify dependencies (git-, http-URLs, TGZ files): https://docs.npmjs.com/files/package.json#dependencies

这篇关于如何自动链接本地 npm 包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆