简体   繁体   English

npm update取消链接包的链接

[英]npm update unlinks linked packages

I have a project, which consists of one root node package containing subpackages linked together by npm link - these subpackages depend on each other (listed in package.json dependencies) and the structure basically looks like this: 我有一个项目,它包含一个根节点包,其中包含通过npm链接链接在一起的子包 - 这些子包彼此依赖(在package.json依赖项中列出),结构基本上如下所示:

-rootpackage
--subpackageA
--subpackageB

Lets say subpackageA has dependency on subpackageB, so I link them to avoid publishing/reinstalling subpackageB in subpackageA after every change in the source of subpackageB. 让我们说subpackageA依赖于subpackageB,所以我链接它们以避免在subpackageB源的每次更改后在subpackageA中发布/重新安装subpackageB。 The link works just fine until I run npm update in subpackageA, which causes the subpackageB to be unlinked. 链接工作正常,直到我在subpackageA中运行npm update ,这导致subpackageB被取消链接。

Now, I see two options: 现在,我看到两个选择:

  1. I can theoretically run the npm link operation after each npm install or npm update to ensure the links are always present. 理论上,我可以在每次npm installnpm update之后运行npm link操作,以确保链接始终存在。 This works with postinstall in case of installation, but in case of an update the postinstall is not called. 这适用于postinstall ,但是在更新的情况下,不会调用postinstall I don't know any postupdate command for npm, which is to be called after update. 我不知道npm的任何postupdate命令,它将在更新后调用。
  2. Maybe there is a way to do this more cleverly, perhaps with yarn , which I am also using, in a way, that it kind of prevents unlinking or excludes the update for my subpackages, so I don't lose the links between my subpackages, but right now I am not aware of such a way. 也许有一种方法可以更巧妙地做到这一点,也许是使用纱线 ,我也在使用,在某种程度上,它可以防止取消链接或排除我的子包的更新,所以我不会丢失我的子包之间的链接,但是现在我不知道这样的方式。

Is there any way to make one of those options work or any other way to solve this problem ? 有没有办法让其中一个选项工作或任何其他方式来解决这个问题? I need to keep this and other links so we don't have to run npm link after every installation/update. 我需要保留此链接和其他链接,这样我们就不必在每次安装/更新后运行npm link I can't really find information about this issue anywhere. 我无法在任何地方找到有关此问题的信息。 Btw I am using Node 6.4.0 and NPM 3.10.3. 顺便说一下,我使用的是Node 6.4.0和NPM 3.10.3。

So the solution is to use Yarn Workspaces or maybe project like Lerna . 所以解决方案是使用Yarn Workspaces或者像Lerna这样的项目。

Yarn Workspaces is a utility that expects a structure similar to what was described in the question and which maintains the linking subpackages and root automatically. Yarn Workspaces是一个实用程序,它需要一个类似于问题中描述的结构,并自动维护链接子包和root。 It is very easy to set up (just 2 lines in root package.json and executing yarn for the first time) and after it you don't have to worry about upgrade or install at all, the links stay in place unless you delete them manually. 它很容易设置(在root.json中只有2行并且第一次执行yarn )并且在它之后你不必担心upgradeinstall ,除非你删除它们,否则链接保持不变手动。

Lerna expands on that and provides you with additional tooling for managing multipackage projects. Lerna对此进行了扩展,并为管理多包装项目提供了额外的工具。 It can use Yarn Workspaces internally for the linking if you use yarn but it is not a requirement and works fine with npm . 如果您使用纱线,它可以在内部使用纱线工作区进行链接,但这不是必需的,并且可以在npm下正常工作。 Just make sure to have Git because last time I checked Lerna didn't work with SVN or other VCSs. 请确保拥有Git,因为上次我检查过Lerna无法使用SVN或其他VCS。

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

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