简体   繁体   English

nodejs使用npm + package.json解析依赖关系

[英]nodejs resolving dependency using npm + package.json

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. 当我在项目根目录上执行npm install ,解决了/package.json中提到的依赖项,但没有解析node_modules / Service_A / package.json或node_modules / Service_B / package.json中的依赖项。 How can I make npm to resolve dependencies across different folders? 如何让npm解决不同文件夹之间的依赖关系?

Service_A and Service_B are local modules I had preloaded inside node_modules [they have dependencies]. Service_A和Service_B是我在node_modules中预加载的本地模块[它们具有依赖关系]。 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. 我知道我可以接受它们的依赖并将它们放在顶层json中,但是如果它们依赖于相同的模块但不同的版本会怎样。 Ex: Service_A requires jquery 1.6 and Service_B jquery 1.7? 例如:Service_A需要jquery 1.6和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. 由于Service_A和Service_B是本地模块,我假设您没有在顶级package.json dependecies部分中定义。 So npm don't know they even exists. 所以npm不知道它们甚至存在。

Consider developing your local modules under git repository, then you are able to define them in following way: 考虑在git存储库下开发本地模块,然后您可以通过以下方式定义它们:

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

You could add something into into your package to call npm install on those package.json files . 您可以在包中添加一些内容,以便在这些package.json文件上调用npm install。 Something like below might do the trick. 像下面这样的东西可能会成功。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM