简体   繁体   English

如何使用NPM处理Node.js中的本地包依赖关系

[英]How to deal with local package dependencies in nodejs with npm

How should we deal with local packages that are a dependency in other local packages? 我们应该如何处理依赖于其他本地包的本地包?

For simplicities sake, say we have the follow packages 为简单起见,请说我们有以下软件包

  • api - express application api快速申请
  • people - a package to deal with people people -与人打交道的套餐
  • data-access - a package that deals with data access data-access -处理数据访问的软件包

And then the dependencies are 然后依赖项是

  • api depends on people api取决于people
  • people depends on data-access people依赖data-access

Currently we have these dependencies setup as file dependencies. 当前,我们将这些依赖项设置为文件依赖项。

Ie api package.json would have api package.json将有

"dependencies": {
  "people": "file:../people"
}

Trouble with this is that we're finding it a PITA when we make updates to one package and want those changes in the other packages that depend on it. 麻烦的是,当我们更新一个程序包并希望依赖它的其他程序包中的那些更改时,我们发现它是PITA。

The options we have thought of are: 我们想到的选项是:

  • npm install - but this won't overwrite previously installed packages if changes are made, so we have to delete the old one from the node_modules directory and re-run npm install... which can be niggly if the package dependency is deep. npm install-但是如果进行更改,这将不会覆盖以前安装的软件包,因此我们必须从node_modules目录中删除旧的软件包,然后重新运行npm install ...如果软件包依赖关系很深,这可能会很麻烦。

  • npm link - we're not sold on the idea because it doesn't survive version control... Just thinking about it now, maybe we have some kind of local build script that would run the npm link commands for us... this way it could survive version control. npm链接 -我们不愿接受这个想法,因为它无法在版本控制中生存...现在考虑一下,也许我们有某种本地构建脚本可以为我们运行npm link命令...这它可以幸免于版本控制的方式。 Would that be a grunt job? 那会是艰巨的工作吗?

  • grunt - we haven't dived too deep into this one yet, but it feels like a good direction. 咕unt咕 --我们还没有深入研究这个问题,但是感觉像是一个很好的方向。 A little bit of googling we came accross this: https://github.com/ahutchings/grunt-install-dependencies 我们在这里进行了一些谷歌搜索: https//github.com/ahutchings/grunt-install-dependencies

So, what option would work best for our situation? 那么,哪种选择最适合我们的情况?
Are there other options that we haven't thought of yet? 还有我们还没有想到的其他选择吗?

Ps. PS。 we're a .NET shop doing a PoC in node, so assume we know nothing! 我们是一家在节点中进行PoC的.NET商店,所以假设我们一无所知!

Pps. PPS。 if you strongly believe we're setting up our project incorrectly and we shouldn't have smaller individual packages, let me know in the comments with a link to some reading on the subject. 如果您坚信我们的项目设置不正确,并且我们不应该使用较小的单个程序包,请在评论中告诉我,并提供指向该主题的阅读链接。

So, I agree that going with 'many small packages' is usually a good idea. 因此,我同意使用“许多小包装”通常是一个好主意。 Check out 12factor.net if you haven't already. 如果尚未登录,请查看12factor.net。

That said, in specific answer to your question I'd say your best bet is to consider mainly how you want to maintain them. 就是说,在具体回答您的问题时,我想最好的选择是主要考虑如何维护它们。

If the 'subcomponents' are all just parts of your app (as, for example, data-access implies), then I'd keep them in the same folder structure, not map them in package.json at all, and just require them where you need them. 如果“子组件”都只是应用程序的一部分(例如,数据访问意味着),那么我会将它们保留在相同的文件夹结构中, 而不是将它们完全映射到package.json中,而只require它们您需要它们的地方。 In this case, everything versions together and is part of the same git repository. 在这种情况下,所有版本都一起发布,并且属于同一git存储库。

If you really want to or need to keep them all in separate git repositories, then you can do npm link, but to be honest I've found it more useful to just use the URL syntax in package.json: 如果您确实想要或需要将它们全部保存在单独的git存储库中,则可以进行npm链接,但是老实说,我发现仅使用package.json中的URL语法更为有用:

dependencies: {
  "people" : "git://path.to.git:repo@version.number"
}

Then, when you want to explicitly update one of your dependencies, you just have to bump the version number in your package.json and run npm install again. 然后,当您想显式更新其中一个依赖项时,只需更改package.json中的版本号,然后再次运行npm install

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

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