简体   繁体   English

pnpm 为相同依赖的完全相同版本提供不同的哈希,打破了nestjs

[英]pnpm provides different hashes for the exact same version of the same dependency, breaking nestjs

I have a monorepo with a very basic setup available for reproducing this issue here:我有一个带有非常基本设置的 monorepo,可用于在此处重现此问题:

It is a single nestjs app with 2 packages that it reads from.它是一个单一的nestjs 应用程序,它有2 个从中读取的包。

@nestjs/core among other dependencies is needed for both the packages and the main app to work, and it is enforced to be the exact same fixed version not only on their own local package.json's but also with the resolutions {} config in the main package.json.包和主应用程序都需要@nestjs/core以及其他依赖项才能工作,并且不仅在它们自己的本地 package.json 上而且在resolutions {}配置中强制它是完全相同的固定版本主要 package.json。

I can inspect the lockfile and find out that although the same version is used -- the hashes are different, causing major issues with nestjs, not being able to import injectable dependencies reliably causing it to break on bootstrap.我可以检查锁定文件并发现尽管使用了相同的版本——哈希值不同,导致nestjs出现重大问题,无法可靠地导入可注入依赖项,导致它在引导时中断。

Is there a way to prevent this?有没有办法防止这种情况? to force linking the exact same hash/dependency?强制链接完全相同的哈希/依赖关系?

When a dependency has peer dependencies, it might be written to node_modules several times if the peer dependencies are resolved differently in various parts of the dependency graph.当依赖项具有对等依赖项时,如果对等依赖项在依赖关系图的各个部分中以不同方式解析,它可能会被多次写入 node_modules。

In your case, @nestjs/core is in the dependencies of the graphql-server project and the @myapp/entities project.在您的情况下, @nestjs/core位于graphql-server项目和@myapp/entities项目的依赖项中。 @nestjs/core has @nestjs/platform-express as an optional peer dependency. @nestjs/core具有@nestjs/platform-express作为可选的对等依赖项。

@nestjs/platform-express is in the dependencies of the graphql-server project, so pnpm links it to @nestjs/platform-express . @nestjs/platform-express位于graphql-server项目的依赖项中,因此 pnpm 将其链接到@nestjs/platform-express You can see it in the lockfile:您可以在锁定文件中看到它:

  /@nestjs/core/8.4.7_fkqgj3xrohk2pflugljc4sz7ea:
    resolution: {integrity: sha512-XB9uexHqzr2xkPo6QSiQWJJttyYYLmvQ5My64cFvWFi7Wk2NIus0/xUNInwX3kmFWB6pF1ab5Y2ZBvWdPwGBhw==}
    requiresBuild: true
    peerDependencies:
      '@nestjs/common': ^8.0.0
      '@nestjs/microservices': ^8.0.0
      '@nestjs/platform-express': ^8.0.0
      '@nestjs/websockets': ^8.0.0
      reflect-metadata: ^0.1.12
      rxjs: ^7.1.0
    peerDependenciesMeta:
      '@nestjs/microservices':
        optional: true
      '@nestjs/platform-express':
        optional: true
      '@nestjs/websockets':
        optional: true
    dependencies:
      '@nestjs/common': 8.4.7_47vcjb2de6lyibr6g4enoa5lyu
      '@nestjs/platform-express': 8.4.7_7tsmhnugyerf5okgqzer2mfqme # <------HERE
      '@nuxtjs/opencollective': 0.3.2
      fast-safe-stringify: 2.1.1
      iterare: 1.2.1
      object-hash: 3.0.0
      path-to-regexp: 3.2.0
      reflect-metadata: 0.1.13
      rxjs: 7.5.5
      tslib: 2.4.0
      uuid: 8.3.2
    transitivePeerDependencies:
      - encoding

In the other project ( @myapp/entities ), @nestjs/platform-express is not in the dependencies, so when installing @nestjs/core , pnpm cannot resolve the optional peer dependency.在另一个项目( @myapp/entities )中, @nestjs/platform-express不在依赖项中,因此在安装@nestjs/core时,pnpm 无法解析可选的对等依赖项。 As a result, pnpm needs to create another instance of @nestjs/core , which doesn't have this optional peer linked in. As you can see in the lockfile, the other entry doesn't have @nestjs/platform-express :因此,pnpm 需要创建另一个@nestjs/core实例,它没有链接这个可选的对等点。正如您在锁定文件中看到的那样,另一个条目没有@nestjs/platform-express

  /@nestjs/core/8.4.7_g7av3gvncewo44y4rurz3mgav4:
    resolution: {integrity: sha512-XB9uexHqzr2xkPo6QSiQWJJttyYYLmvQ5My64cFvWFi7Wk2NIus0/xUNInwX3kmFWB6pF1ab5Y2ZBvWdPwGBhw==}
    requiresBuild: true
    peerDependencies:
      '@nestjs/common': ^8.0.0
      '@nestjs/microservices': ^8.0.0
      '@nestjs/platform-express': ^8.0.0
      '@nestjs/websockets': ^8.0.0
      reflect-metadata: ^0.1.12
      rxjs: ^7.1.0
    peerDependenciesMeta:
      '@nestjs/microservices':
        optional: true
      '@nestjs/platform-express':
        optional: true
      '@nestjs/websockets':
        optional: true
    dependencies:
      '@nestjs/common': 8.4.7_47vcjb2de6lyibr6g4enoa5lyu
      '@nuxtjs/opencollective': 0.3.2
      fast-safe-stringify: 2.1.1
      iterare: 1.2.1
      object-hash: 3.0.0
      path-to-regexp: 3.2.0
      reflect-metadata: 0.1.13
      rxjs: 7.5.5
      tslib: 2.4.0
      uuid: 8.3.2
    transitivePeerDependencies:
      - encoding

To solve this, you can add @nestjs/platform-express to the dependencies of the @myapp/entities project.要解决这个问题,您可以将@nestjs/platform-express添加到@myapp/entities项目的依赖项中。 It should be the same version as in the other project.它应该与其他项目中的版本相同。

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

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