A colleague's dependency is taking precedence over an NX Library

I'm working in a monorepo environment with nx, structured as follows:

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

After deployment, the libraries are published on npm with the names @my-org/my-client and @my-org/common. I have set up path aliases in tsconfig.conf to directly reference them in my-app code:

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

The problem arises when my-app uses an external package another-external-package that relies on the published version of @my-org/common.

It appears that when I import @my-org/common in my-app, it defaults to the peer dependency of @my-org/common from another-external-package, instead of using the alias defined in tsconfig.conf.

This issue only occurs in production builds, not in the development environment. Any suggestions on how to instruct nx/tsc to prioritize the library over the published package?

Answer №1

One possible solution is to utilize package overrides.

Consider adding the following code snippet to your package.json file: (not guaranteed to be accurate for your specific scenario)

  "overrides": {
    "@my-organization/my-app": {
      "@my-organization/shared": "$@my-organization/shared"
    }
  }

To learn more about this concept, check out: https://docs.npmjs.com/cli/v8/configuring-npm/package-json#overrides

When you need to make precise modifications to dependencies within your project's dependencies, such as updating a dependency with a known security vulnerability, substituting an existing dependency with a fork, or ensuring uniformity in package versions across the board, implementing an override can be advantageous.

Overrides offer a means to swap out a package in your dependency hierarchy with an alternate version or even an entirely different package. The scope of these adjustments can be tailored to suit your specific requirements.

Answer №2

I was able to solve the issue by directly adding the dependencies indicated in the tsconfig.conf file to the package.json. Here's how I did it:

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

By doing this, my my-app now utilizes the local version of @my-org/common instead of relying on the peer dependency resolved through @my-org/my-client.

As mentioned by @EcksDy:

The aliases in tsconfig.paths are only for your IDE. The resolve occurs at build time using the ts-loader package, especially if you're using webpack, which is the default configuration for nx apps.

@johey: This isn't about replacing the package called in the dependencies, but rather instructing the app to use the local version instead of the published one :)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble executing a script in npm to launch testcafe

I'm currently working on a React project where I am utilizing an npm script to run tests using testcafe. These tests involve the use of yargs and shelljs. The issue arises when running the tests through the command npm run test:local -- --api=local, a ...

Encountering an EACCES issue while attempting to download firebase-tools onto Mac Big Sur

Trying to install firebase-tools on my Mac with Big Sur 11.1 and LTS Version: 14.17.1 (includes npm 6.14.13) has been unsuccessful. Both attempts with and without sudo resulted in ERR! code EACCES. Here are the details: % npm install -g firebase-tools np ...

What is the reason behind tsc disregarding the include and exclude options in tsconfig.json?

I am facing an issue with my tsconfig.json file: { "compilerOptions": { "target": "ES6", "lib": [ "DOM", "ES6" ] }, "include": [ "src/server/**/*&q ...

`Set-cookie` isn't effective in dist compilation produced by the `npm run build` command

Currently, my vue frontend server utilizes cookies to manage the login state in conjunction with a basic backend server. The issue arises when set-cookie functions properly in production mode while running npm run serve. However, upon bundling the project ...

Nuxt encountered an issue with Vue hydration: "Tried to hydrate existing markup, but the container is empty. Resorting to full mount instead."

I'm facing an issue while trying to integrate SSR into my project. I keep encountering this error/warning. How can I pinpoint the problem in my code? There are numerous components in my project, so I'm unsure if I should share all of my code, b ...

Setting an expiry date for Firestore documents

Is it feasible to set a future date and time in a firestore document and trigger a function when that deadline is reached? Let's say, today I create a document and specify a date for the published field to be set to false fifteen days later. Can this ...

Tips on using services for storing and fetching list data in Angular

I currently have two components, the "add-expense" component and the "view-list" component. The "add-expense" component collects expense details from a form and stores them as an object. My goal is to add this object to an empty list within the "expense-li ...

Issue with click function not activating in Chrome when using Angular 6

I am facing an issue where the (click) function is not triggering in my select tag when I use Google Chrome, but it works fine in Mozilla. Below is my code: <div class="col-xl-4 col-lg-9"> <select formControlName="deptId" class="form-control ...

Which is better suitable in TypeScript generics: void, never, or undefined?

Curious about this topic. I am seeking a definitive answer to clarify my understanding. My goal is to specify to the compiler/language service/reader that T should be absolutely nothing, empty, or null. I am unsure whether void, never, or undefined is the ...

Explain the concept of a static async create method having identical parameters as the constructor

I have a lot of existing classes that require refactoring to utilize an async constructor. Here's an example: class ClassA { constructor(a: number, b: string, c: string) { //... } //... } I've included an async create method ...

Tips for Maintaining User Sessions in Ionic 4 Application

I am facing an issue with keeping users logged in to the app in Ionic 4. I have tried storing credentials like email and cross-checking the user using an API, then redirecting to the menu page. However, I am experiencing a brief moment on the login page be ...

The error TS2300 arises from the duplicate identifier 'PropertyKey' found in the node_modules/@types/core-js/index.d.ts file

I am encountering errors in the file node_modules/@types/core-js/index.d.ts while using Visual Studio Code IDE: https://i.sstatic.net/fkAej.png After running npm start to serve the application, the following errors are displayed: (list of errors her ...

Converting JSON to TypeScript in an Angular project

Within my Angular project, I have an HTTP service that communicates with a web API to retrieve JSON data. However, there is a discrepancy in the naming convention between the key in the JSON response (e.g., "Property" in uppercase) and the corresponding pr ...

Leverage the useParams data to serve as a state object key in the useSelector function using TypeScript

Looking to access state data using a key obtained from useParams? Here's an example: export const MainPageSection = (props:MainPageSectionPropsType) => { const params = useParams(); const currentSection = params.section const excursions ...

The type '{}' is lacking the 'submitAction' property, which is necessary according to its type requirements

I'm currently diving into the world of redux forms and typescript, but I've encountered an intriguing error that's been challenging for me to resolve. The specific error message reads as follows: Property 'submitAction' is missing ...

Deliver either a Promise or a boolean based on the provided parameters, utilizing a default

I have some code that can either return a boolean or a Promise depending on the parameters provided. function setGuid<B extends boolean>(guid: string, shouldValidate?: B): B extends true ? boolean : Promise<boolean> function setGuid(guid: strin ...

Next.js encountered an error when trying to locate the 'net' module while working with PostgreSQL

I'm facing a challenge in my Next.js project while attempting to retrieve all records from a table. The error message I'm encountering is "Module not found: Can't resolve 'net'" with an import trace pointing to multiple files withi ...

What could be causing the npm start command to not function as expected?

After beginning a new project in React, I encountered an issue with npm start not functioning properly. While I was able to troubleshoot installation problems previously, this time I have tried multiple solutions without success. As I am unable to upload ...

Managing animations with multiple components in Angular 2+

I am currently developing an Angular application that will utilize a series of Modals in a wizard-style setup. For this project, I am utilizing the Angular-cli tool. Below is the code snippet showing how I have set up my animations: animations:[ t ...

The wildcard syntax for importing statements in Angular 2

I created multiple classes in a single file with the following structure file: myclasses.ts export class Class1 {....} export class Class2 {....} export class Class3 {....} Now I am attempting to import all of them using a wildcard like this import {*} ...