【问题标题】:nodejs resolving dependency using npm + package.jsonnodejs 使用 npm + package.json 解决依赖关系
【发布时间】:2014-06-30 20:37:58
【问题描述】:

我的项目结构类似于

 /
    index.js
    package.json
    node_modules
    |_Service_A
      |__main.js
      |__package.json
    |_Service_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?

【问题讨论】:

    标签: node.js npm


    【解决方案1】:

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

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

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

    【讨论】:

    【解决方案2】:

    你可以在你的包中添加一些东西来调用那些 package.json 文件的 npm install 。像下面这样的东西可能会奏效。

    "scripts": {
       "preinstall": "npm install Service_A/ && npm install Service_B/"
    }
    

    【讨论】:

      猜你喜欢
      • 2017-09-25
      • 2013-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-18
      • 1970-01-01
      • 2014-12-18
      • 1970-01-01
      相关资源
      最近更新 更多