A peerDependency is overriding a NX Library

Viewed 120

I'm using a monorepo with nx, with the following structure:

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:

"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).

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. 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?

2 Answers

I think you can use package overrides.

Try to add this in your package.json file: (not sure if I've put it correctly for your situation)

  "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

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.

Overrides provide a way to replace a package in your dependency tree with another version, or another package entirely. 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:

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

As @EcksDy mentioned:

The aliases in tsconfig.paths are only for your IDE. The resolve happens at build time via ts-loader package in case you're using webpack which is default nx configuration for apps.

@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 :)

Related