nodejs resolving dependency using npm + package.json

时光怂恿深爱的人放手 提交于 2021-02-08 20:48:08

问题


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?


回答1:


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"
}



回答2:


You could add something into into your package to call npm install on those package.json files . Something like below might do the trick.

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


来源:https://stackoverflow.com/questions/11135383/nodejs-resolving-dependency-using-npm-package-json

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!