Trouble with importing a package using path aliases in a Monorepo setup with Lerna and TypeScript

My current challenge involves setting up a TypeScript based monorepo using Lerna. In this setup, I have two packages named bar and foo. The issue arises when foo tries to import bar using a path alias and encounters an error.

  1. tree
.
├── lerna.json
├── package.json
└── More file structure ...

Among the key configuration files:

  1. ./tsconfig.build.json
{
  "compilerOptions": {
    "baseUrl": ".",
    "target": "es6",
    "module": "commonjs",
    "moduleResolution": "node",
    "declaration": true
  }
}
  1. ./tsconfig.json
{
  "extends": "./tsconfig.build.json",
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@company/bar": [
        "packages/bar"
      ],
      "@company/foo": [
        "packages/foo"
      ]
    }
  }
}
  1. ./lerna.json
{
  "packages": [
    "packages/*"
  ],
  "version": "0.0.0"
}
  1. ./package.json
{
  "name": "root",
  "private": true,
  "scripts": {
    "tsc": "lerna run tsc"
  },
  "devDependencies": {
    "lerna": "^3.22.1",
    "ts-node": "^9.0.0",
    "ts-node-dev": "^1.0.0-pre.63",
    "typescript": "^4.0.3"
  }
}

In the case of Package bar:

  1. File paths and configurations for bar package...

And for Package foo:

  1. File paths and configurations for foo package...

When running npm run tsc, the console outputs an error when foo attempts to import bar:

Error message detailing the module import issue...

The error is well defined, but I am unsure how to resolve it considering that my path aliases in ./tsconfig.json (3) seem correct. Can anyone identify where I might be going wrong with my configurations? Any insights or suggestions on fixing this would be greatly appreciated.

Although switching the import statement from

import { bar } from '@company/bar';
to
import { bar } from '../../bar/src';
resolves the issue, I prefer sticking to the initial way of importing bar.

Answer №1

In order to import packages as @company/bar, you need to configure the paths like this:

    "paths": {
      "@company/bar": [
        "packages/bar"
      ],
      "@company/foo": [
        "packages/foo"
      ]
    }

This setup is defined in ./tsconfig.json. However, when compiling package foo, it references

./packages/foo/tsconfig.build.json
which does not include the root config or specify the paths itself. To resolve this issue, consider removing the import scheme from the base tscofig and adding them to the inner package configs as follows:

./packages/foo/tsconfig.build.json
should contain:

{
  "compilerOptions": {
    "paths": {
      "@company/bar": [
       "../bar"
      ]
    }
  }
}

Similarly, packages/bar/tsconfig.build.json should allow importing @company/foo if necessary.

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

Transferring object information to Backand using Ionic 2

I have developed a signup page using Ionic 2. In this signup page, I have included a dropdown menu for users to select their blood type. However, I am facing an issue where the selected blood type is not being sent to the Backand database as expected. I&ap ...

Why isn't the unit test passing when it clearly should be?

I am encountering an issue with testing a simple function that I have created. Despite the fact that the function works correctly in practice, it is not being tested properly... Explanation of how my function operates (While it functions as intended, the ...

Swapping JSON: A Quick Guide

When my Angular code loads, a list of buttons (button 1, button 2, button 3, etc.) is displayed. Upon clicking any button, the console shows J-SON with varying values. Two additional buttons are present on the page for moving up and down. My dilemma arise ...

Monitoring changes within the browser width with Angular 2 to automatically refresh the model

One of the challenges I faced in my Angular 2 application was implementing responsive design by adjusting styles based on browser window width. Below is a snippet of SCSS code showing how I achieved this: .content{ /*styles for narrow screens*/ @m ...

Creating a custom type for the parameter of an arrow function in Typescript

I need assistance defining the type for an object parameter in an arrow function in TypeScript. I am new to TypeScript and have not been able to find any examples illustrating this scenario. Here is my code: const audioElem = Array.from(videoElem.pare ...

Converting text data into JSON format using JavaScript

When working with my application, I am loading text data from a text file: The contents of this txt file are as follows: console.log(myData): ### Comment 1 ## Comment two dataone=1 datatwo=2 ## Comment N dataThree=3 I am looking to convert this data to ...

What causes the inconsistency in TypeScript's structure typing?

It is well-known that TypeScript applies structure typing, as demonstrated in the following example: interface Vector { x: number; y: number; } interface NamedVector { x: number; y: number; name: string; } function calculateLength(v: Vecto ...

Developing a React-based UI library that combines both client-side and server-side components: A step-by-step

I'm working on developing a library that will export both server components and client components. The goal is to have it compatible with the Next.js app router, but I've run into a problem. It seems like when I build the library, the client comp ...

Guide on implementing a cordova plugin in a TypeScript cordova application

In my Cordova project, which utilizes Angular and Typescript, I am facing issues with implementing the juspay-ec-sdk-plugin for Cordova. I have explored possible solutions on StackOverflow related to integrating Cordova plugin in an Angular 4 Typescript ...

Utilize Material-UI in Reactjs to showcase tree data in a table format

I am currently tackling a small project which involves utilizing a tree structure Table, the image below provides a visual representation of it! click here for image description The table displayed in the picture is from my previous project where I made ...

When using TypeScript and Material UI, it is important to assign a value to boolean attributes

Trying to implement Material UI code with Typescript for a DisplayCard component, but encountering an error message: (34,23): Value must be set for boolean attributes. The challenge lies in identifying which attribute value is missing... Here is the samp ...

Finding it challenging to adapt an AngularJs component-based modal to TypeScript

When creating an AngularJS component in JavaScript and displaying it as a modal using ui-bootstrap, you need to pass bindings that the modal instance can use for dismissing or closing itself: app.component("fringeEdit", { controller: "FringeEditCont ...

Stopping npm build when ESLint detects warnings

Dealing with a particularly immature team, I am determined to make the react-typescript build fail whenever ESLint issues warnings. src/modules/security/components/ForgotPasswordBox/index.tsx Line 8:18: 'FormikHelpers' is defined but never use ...

Executing a single method during the ngAfterViewChecked lifecycle hook in Angular

When tracking a change of variable and wanting to run a function after the change, I utilized the ngAfterViewChecked hook. However, since this method involves data submission which should only occur once, I incorporated a boolean status variable to manage ...

"Running into Memory Limitations: Resolving System.OutOfMemoryException in ASP.NET Web API Integrated with Angular

Currently, I am working on integrating Angular 2+ with asp.net Web API. The issue I am facing is related to dealing with a C# DataTable that is being returned to Angular 2+. To do this, I am utilizing the Post method. The problem arises when I try to retr ...

Creating two number-like types in TypeScript that are incompatible with each other can be achieved by defining two

I've been grappling with the challenge of establishing two number-like/integer types in TypeScript that are mutually incompatible. For instance, consider the following code snippet where height and weight are both represented as number-like types. Ho ...

How can I capture the click event on the oktext in Ionic?

When using Ionic, I have a select button with options for okText and cancelText. The issue I am facing is that when I click on okText, the menu closes as expected due to this attribute. However, I am interested in implementing it through click events. Belo ...

Typescript mistakenly labels express application types

Trying to configure node with typescript for the first time by following a tutorial. The code snippet below is causing the app.listen function to suggest incorrectly (refer to image). import express from 'express'; const app = express(); app.li ...

Unable to transfer props object to React component using TypeScript

Apologies if this seems like a basic issue, but I've been struggling with it for the past two days. I'm currently working on writing a simple unit test in Vitest that involves rendering a component to the screen and then using screen.debug(). The ...

`Implementing Typescript code with Relay (Importing with System.js)`

Is there a way to resolve the error by including system.js or are there alternative solutions available? I recently downloaded the relay-starter-kit (https://github.com/relayjs/relay-starter-kit) and made changes to database.js, converting it into databas ...