nodejs 使用 npm + package.json 解析依赖 [英] nodejs resolving dependency using npm + package.json

查看:60
本文介绍了nodejs 使用 npm + package.json 解析依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目结构如下

<块引用>

<代码>/索引.js包.json节点模块|_服务_A|__main.js|__package.json|_服务_B|__main.js|__package.json

当我在我的项目根目录上执行 npm install 时,/package.json 中提到的依赖项已解决,但 node_modules/Service_A/package.json 或 node_modules/Service_B/package.json 中的依赖项未解决.如何让 npm 解决不同文件夹之间的依赖关系?

Service_A 和 Service_B 是我在 node_modules 中预加载的本地模块 [它们有依赖项].我知道我可以将它们的依赖项放在顶级 json 中,但是如果它们依赖于相同的模块但不同的版本怎么办.例如:Service_A 需要 jquery 1.6 和 Service_B jquery 1.7?

解决方案

因为 Service_A 和 Service_B 是本地模块,我假设它们未在您的顶级 package.json 依赖项部分中定义.所以 npm 甚至不知道它们存在.

考虑在 git 存储库下开发本地模块,然后您可以通过以下方式定义它们:

依赖项":{"public": "git://github.com/user/repo.git#ref","private": "git+ssh://git@github.com:user/repo.git#ref"}

I have structured my project like

 /
    index.js
    package.json
    node_modules
    |_Service_A
      |__main.js
      |__package.json
    |_Service_B
      |__main.js
      |__package.json

When I do npm install on my project root directory the dependencies mentioned in /package.json is resolved but not the ones in node_modules/Service_A/package.json or node_modules/Service_B/package.json. How can I make npm to resolve dependencies across different folders?

Service_A and Service_B are local modules I had preloaded inside node_modules [they have dependencies]. I know I can take their dependency and put them in the top level json only, but what if they have dependency over same module but different versions. Ex: Service_A requires jquery 1.6 and Service_B jquery 1.7?

解决方案

As Service_A and Service_B are local modules i assume that are not defined in your top level package.json dependecies section. So npm don't know they even exists.

Consider developing your local modules under git repository, then you are able to define them in following way:

"dependencies": {
  "public": "git://github.com/user/repo.git#ref", 
  "private": "git+ssh://git@github.com:user/repo.git#ref"
}

这篇关于nodejs 使用 npm + package.json 解析依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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