简体   繁体   English

peerDependency 覆盖 NX 库

[英]A peerDependency is overriding a NX Library

I'm using a monorepo with nx, with the following structure:我正在使用带有 nx 的 monorepo,其结构如下:

apps
  | - my-app
libs
  | - common
  | - my-client

The libs are being published on npm after the deployment under the names of @my-org/my-client and @my-org/common , while I'm defining the following path alias (on tsconfig.conf ) to use them directly on my-app code:在以@my-org/my-client@my-org/common的名称部署后,这些库正在 npm 上发布,而我正在定义以下路径别名(在tsconfig.conf上)以直接使用它们my-app代码:

"paths": {
      "@my-org/my-client": ["libs/my-client/src/index.ts"],
      "@my-org/common": ["libs/common/src/index.ts"]
}

The issue is that my-app is using an external package another-external-package that depends on @my-org/common (it's importing with its published version).问题是my-app正在使用依赖于@my-org/common的外部 package another-external-package (它正在与其发布的版本一起导入)。

When I import @my-org/common on my-app , it seems that it's picking up the peer dependency @my-org/common (from another-external-package ) and not from the alias that is defined on tsconfig.conf .当我在my-app上导入@my-org/common时,它似乎正在获取对等依赖@my-org/common (来自another-external-package ),而不是来自tsconfig.conf上定义的别名。 This happens only when we build for production but not in the dev environment.这只发生在我们为生产而不是在开发环境中构建时。

Any idea on how to tell nx/tsc to pick the library instead of the published package?关于如何告诉 nx/tsc 选择库而不是已发布的 package 的任何想法?

I think you can use package overrides .我认为您可以使用package 覆盖

Try to add this in your package.json file: (not sure if I've put it correctly for your situation)尝试将其添加到您的 package.json 文件中:(不确定我是否根据您的情况正确放置)

  "overrides": {
    "@my-org/my-client": {
      "@my-org/common": "$@my-org/common"
    }
  }

It's explained here: https://docs.npmjs.com/cli/v8/configuring-npm/package-json#overrides这里解释: https://docs.npmjs.com/cli/v8/configuring-npm/package-json#overrides

If you need to make specific changes to dependencies of your dependencies, for example replacing the version of a dependency with a known security issue, replacing an existing dependency with a fork, or making sure that the same version of a package is used everywhere, then you may add an override.如果您需要对依赖项的依赖项进行特定更改,例如用已知安全问题替换依赖项的版本,用分叉替换现有依赖项,或者确保在任何地方都使用相同版本的 package,那么你可以添加一个覆盖。

Overrides provide a way to replace a package in your dependency tree with another version, or another package entirely.覆盖提供了一种将依赖关系树中的 package 替换为另一个版本或完全替换为另一个 package 的方法。 These changes can be scoped as specific or as vague as desired.这些更改的范围可以根据需要具体或模糊。

I found a fix by adding directly the dependencies that are marked on the tsconfig.conf into the package.json , like this:我通过将 tsconfig.conf 上标记的依赖项直接添加到tsconfig.conf中找到了解决package.json ,如下所示:

"dependencies": {
    "@my-org/common": "file:./libs/common"
}

This made my-app uses the local version of @my-org/common instead of using the peer dependency that was resolved via @my-org/my-client .这使得my-app使用@my-org/common的本地版本,而不是使用通过@my-org/my-client解决的对等依赖项。

As @EcksDy mentioned:正如@EcksDy 提到的:

The aliases in tsconfig.paths are only for your IDE. tsconfig.paths 中的别名仅适用于您的 IDE。 The resolve happens at build time via ts-loader package in case you're using webpack which is default nx configuration for apps.如果您使用的是 webpack(这是应用程序的默认 nx 配置),则在构建时通过 ts-loader package 进行解析。

@johey: This is not about overriding the package that is called in the dependencies, but asking the app to use to the local version, and not the published one:) @johey:这不是要覆盖在依赖项中调用的 package,而是要求应用程序使用本地版本,而不是发布的版本:)

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

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