angular-cli build error in monorepo: TS2451: 'ngDevMode' variable cannot be redeclared

In my development environment, I have organized a monorepo with two distinct packages. The first package is the primary application, known as app, while the second package is a shared library that is utilized by the main application, referred to as lib. Both of these packages are TypeScript/Angular projects.

The framework of my project looks like this:

monorepo/
  packages/
    app/
    lib/

I am currently attempting to configure the app project in such a way that when an import from the lib package is detected, instead of searching through the node_modules directory, it will directly access the output directory generated from building the lib package. In order to achieve this, I have made alterations in my tsconfig.json:

{
  "compilerOptions": {
    "baseUrl": ".",
    "rootDir": ".",
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": ["es7", "dom"],
    "mapRoot": "./",
    "module": "es2015",
    "moduleResolution": "node",
    "outDir": "../dist/out-tsc",
    "sourceMap": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "paths": {
      "lib/*": ["../lib/dist/*"]
    }
}

However, upon executing ng build within the app folder, I encounter the following error messages (the same error is reported twice, originating from separate locations):

ERROR in node_modules/@angular/core/src/render3/ng_dev_mode.d.ts(9,11): error TS2451: Cannot redeclare block-scoped variable 'ngDevMode'.
../lib/node_modules/@angular/core/src/render3/ng_dev_mode.d.ts(9,11): error TS2451: Cannot redeclare block-scoped variable 'ngDevMode'.

This issue signifies that Angular is being loaded redundantly, once from the app directory and again from the lib directory.

Answer №1

It appears that the issue is with loading angular twice, as you have pointed out. There are two possible solutions for this problem:

Option 1: enforce the use of the specific angular version defined in the application. Inside the tsconfig.json file of your app, add the following line to the 'paths' property. This will ensure that the angular library used is from the node_modules directory of your project (adjust the relative path as needed)

"paths": {
  ...
  "@angular: ["../node_modules/@angular"],
  ...

Option 2: create a symbolic link to integrate your library into the app.

  1. Build your library, which should be located at my-lib-folder\dist\my-lib
  2. Navigate to your app's folder
  3. Run the command
    npm install ..\my-lib-folder\dist\my-lib
    . This will establish a symlink in the node_modules of your app pointing to the distribution of your library.
  4. Edit the tsconfig.json file and include a path to the distribution of your library
"paths": {
  ...
  "my-lib": ["dist.my-lib"]
  ...

Regardless of the option chosen, pay attention to your imports:

GOOD : import {MyLibModule} from 'my-lib/';
BAD : import {MyLibModule} from '../my-lib-folder/my-lib/...';

In the latter scenario, the angular version will still be loaded based on the relative import location.

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

Adding an active class to a selected list item can easily be accomplished by targeting the

Hey there, I'm attempting to apply a class to the selected list item and also add a class when scrolling to a specific div. For instance, if I scroll to div#six, the number six (6) in the menu should also have the 'active' class. [Check out ...

Finding the correlation between SVG element IDs and JSON keysUnderstanding how to pair up

Currently, I have an SVG file that can be viewed here. My goal is to present specific data when elements within the SVG are clicked. The data is in JSON format and I am looking to match each ID of an SVG element with a key in the JSON data. If both the key ...

Retrieve the modal ID when the anchor tag is clicked in order to open the modal using PHP

I am facing an issue with opening a modal and passing the id value using JavaScript. The id value is shown in a hidden input field. <a href="#modal2" data-toggle="modal" data-id="<?php echo $CRow['id'];?>" id="<?php echo $CRow[& ...

Event triggered when a Socket.IO connection is disconnected

Everything seems to be working well with my code when a new user joins, but I'm encountering issues when a user leaves as it doesn't display anything. Can someone point out the error in my code? Below is my server-side code: const express = requ ...

Executing form actions on click using Ajax without causing a page refresh

I'm currently working on a chat app where users can input their username and message, which should then be inserted into a database. However, I seem to be facing an issue with the code below, as the data is not being successfully submitted: FORM + S ...

How can Observables be designed to exhibit both synchronous and asynchronous behavior?

From: Understanding the Contrasts Between Promises and Observables In contrast, a Promise consistently operates asynchronously, while an Observable can function in synchronous or asynchronous manners. This presents the opportunity to manipulate code in ...

Step-by-step guide on incorporating an external JavaScript library into an Ionic 3 TypeScript project

As part of a project, I am tasked with creating a custom thermostat app. While I initially wanted to use Ionic for this task, I encountered some difficulty in integrating the provided API into my project. The API.js file contains all the necessary function ...

Can you explain the significance of these specific HTML attributes within the button tag?

While browsing the web, I stumbled upon this HTML snippet: <button class="button--standard" mat-button mat-flat-button [disabled]=true >Disabled State</button> The combination of attributes like mat-button mat-flat-button [disabled]=true is u ...

Loading JavaScript in the background using Java and HtmlUnit

I am currently facing a challenge while navigating a website using HtmlUnit. This particular website adjusts certain buttons and displays or hides certain elements based on JavaScript events. For instance, there is a text input box along with a button th ...

Having trouble accessing the POST RESPONSE json in ReactJS and NodeJS

I have set up my own API server that is connected to a MySQL database. Whenever I send a request to the server, everything seems fine. I can see the input from the browser and the output from the database in the console. However, I am unable to see any new ...

Unable to make anchor tag inside button effectively collapse another div

My Nuxt 2 SSR + Bootstrap 5 application includes the following code snippet: <button v-for="file of orderProduct.files" class="collapsed son-collapse" type="button" data-bs-toggle=&quo ...

I can't seem to figure out why the game is not showing up on the desktop in this index HTML

I have come across a game online and I am trying to run it from my desktop. Do I need to do anything special with the URLs or images to ensure that the file recognizes where everything is located? I have all the files and .png images in one folder at the s ...

Why does the jQuery .each loop only loop when I console.log?

Feeling a little confused right now. I have a .each function that successfully displays all results from JSON when I use console.log, but for some reason, when I try to output using .html(), it only shows one result at a time. Any idea why this might be ha ...

Transform an Array<string> into an object with the strings of the array as keys

I am attempting to develop a type conversion method that transforms an array of strings into an object with the string values as keys. type ObjectFromKeys<T extends Array<string>> = { [K in keyof T]: string } declare const o: ObjectFromKeys& ...

What is the best way to develop a node package that is compatible with various operating systems?

I am working on a typescript project that utilizes yarn for building and releasing. One of the dependencies I am using is specifically for "windows only" environments. This requirement affects three key areas: source code, building process, and release tas ...

The global CSS styles in Angular are not being applied to other components as expected

Currently utilizing Angular v10, I have a set of CSS styles that are meant to be used across the entire application. To achieve this, I added them to our global styles.css file. However, I'm encountering an issue where the CSS is not being applied to ...

I am curious as to why jQuery offers the done() method for promises, while the JavaScript promises documented by Mozilla do not. Is there a way to incorporate a done() method in vanilla JavaScript promises if needed?

What sets apart the Promises in Mozilla's JavaScript documentation (view API page) from those in jQuery's Promises (see API page)? It appears that Mozilla's promise only includes 2 methods: then and catch. On the other hand, jQuery's p ...

Error: The checkbox was clicked, but an undefined property (includes) cannot be read

Link to live project preview on CodeSandbox Visit the product page with checkbox I have developed a code snippet that allows users to filter products by checking a box labeled "Show Consignment Products Only", displaying only those products with the term ...

Would it be possible to use a script to convert the y-axis values of the chart into Arabic numerals?

Currently, I am working on creating line and pie charts, but I need the Y-axis to display Arabic language. $('button').on('click', function(e) { $('#pie-chart3').empty(); var type = $(this).d ...

Problem with jQuery Window Resize Trigger Not Reactivating

I am facing a challenge in integrating a slider within multiple jquery tabs. As the slider requires specific width and height to display correctly, I have to trigger a window resize event when the tabs are switched. Surprisingly, the code I implemented onl ...