Compiling an Angular project with an external library in AOT mode using angular-cli is causing issues and not compiling successfully

Embarking on a fresh new project, I utilized angular-cli 8.1.2 for the generation process. The goal is to establish a shared library that caters to multiple microservices (apps). This particular library should remain separate from the applications folder, constituting a distinct project with its own git repository. However, upon attempting this setup, the application encounters compilation issues in aot/production mode, despite functioning fine in jit.

The vision involves having two projects housed in two directories within the application:

/test-lib
/test-app

The initial step entails generating the library using angular-cli:

ng new test-lib --create-application=false
(using defaults)
cd test-lib/
ng generate library test-lib --prefix=test
ng build test-lib

This procedure yields the creation of the library folder along with a project residing at /test-lib/projects/test-lib

Subsequently, the (first) application is generated as follows:

cd ..
ng new test-app
(using defaults)

To link the library with this app:

"paths" must be added to tsconfig.json:

"paths": {
  "test-lib": [
    "../test-lib/dist/test-lib"
  ],
  "test-lib/*": [
    "../test-lib/dist/test-lib/*"
  ]
}

In addition, an import statement must be included in app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { TestLibModule } from 'test-lib';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    TestLibModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Furthermore, the following tag needs to be inserted into app.component.html:

<test-test-lib></test-test-lib>

Upon executing "ng serve," the operation proceeds smoothly without any hitches.

However, running "ng build" within the test-app directory leads to failure and displays the following error message:

ERROR in : Unexpected value 'TestLibModule in C:/Users/.../test-lib/dist/test-lib/test-lib.d.ts' imported by the module 'AppModule in C:/Users/.../test-app/src/app/app.module.ts'. Please add a @NgModule annotation.
enter code here

An additional issue surfaces:

: 'test-test-lib' is not a known element:
1. If 'test-test-lib' is an Angular component, then verify that it is part of this module.
2. If 'test-test-lib' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to 
the '@NgModule.schemas' of this component to suppress this message. ("
</ul>

[ERROR ->]<test-test-lib></test-test-lib>")

Attempts to import the component in app.component.ts also proved unsuccessful (component not found).

Shouldn't there be a straightforward way to develop a library externally in a separate folder using angular-cli?

Answer №1

To utilize the public_api barrel in your library, make sure to import the module from it and then compile the code.

Additionally, utilizing npm link can help manage separate directories for your project.

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

Transitioning a codebase from @angular-builders/custom-webpack to NX for project optimization

I need help migrating my Angular project from using "@angular-builders/custom-webpack" build targets to transitioning to an integrated NX monorepo. When I run the command npx nx@latest init --integrated, I receive the following warning: Unsupported build ...

No TypeScript error in Angular app when assigning a string to a number data type

Today, I encountered some confusion when my app started acting strangely. It turns out that I mistakenly assigned a string to a number without receiving any error alerts. Any thoughts on why this happened? id:number; Later on: this.id = ActiveRoute.params ...

Embedding a transpiled .js file in HTML using ExpressJS as a static resource

ExpressJS setup to serve transpiled TypeScript files is giving me trouble. Whenever I try to access /components/foo.js, I keep getting a 404 error. /* /dist/server.js: */ var express = require('express'); var app = express(); var path = requir ...

Creating a dynamic popup/modal in Angular 7 using a service

As a newcomer to Angular, I find myself with a question regarding services. In the past, when working with other languages, I would create self-contained components that could be called from anywhere within the application. Currently, my need for popup di ...

Ways to avoid Next.js from creating a singleton class/object multiple times

I developed a unique analytics tool that looks like this: class Analytics { data: Record<string, IData>; constructor() { this.data = {}; } setPaths(identifier: string) { if (!this.data[identifier]) this.da ...

Using the Angular async pipe with an object's property

Is there a way to use the async pipe without ngFor? I need to check a property of an object that is asynchronously loaded with an observable. This is what I have tried, but it's not working as expected: <ion-item *ngIf="user$.anonymouse | async"& ...

Encountering difficulty adjusting the size of my charts when attempting to utilize the billboard.js chart library with Angular 6

I am currently working on integrating the billboard.js chart library with Angular 6, but I am encountering an issue with the size of the chart. Upon the initial page load, the chart appears larger than its containing div element. However, when I resize the ...

How to store property transformation in Redux/ngrx

I have the following setup someReducer.ts export interface State { someProp: MyModel; } // some action listeners .. // // export const getProp = (state: State) => state.someProp; selector.ts export const getProperty = createSelector(getState, f ...

What is the recommended approach for testing a different branch of a return guard using jest?

My code testing tool, Jest, is indicating that I have only covered 50% of the branches in my return statement test. How can I go about testing the alternate branches? The code snippet below defines a function called getClient. It returns a collection h ...

Update the nest-cli.json configuration to ensure that non-TypeScript files are included in the dist directory

I've been searching for a solution for hours now: I'm developing an email service using nestJS and nest mailer. Everything was working fine until I tried to include a template in my emails. These templates are hbs files located in src/mail/templ ...

Crafting a Retro Style

I have an interface called Product which includes properties such as name, and I want to track changes to these products using a separate interface called Change. A Change should include the original Product as well as all of its properties prefixed with t ...

Encountering an issue with Angular 13 routing where extraction of property fails when returning value as an observable

After receiving an id number from the parent component, I pass it to my child component in order to retrieve a value with multiple properties. To achieve this, I created a service that returns an observable containing the desired object. However, when atte ...

Creating a new Angular2 component for a separate URL path

In my scenario, I have a PageComponent that gets refreshed with new data when the URL changes (using PageService). It currently utilizes the same PageComponent object, but I am looking to make it a new object when the URL changes. 1st Question: How can ...

What are the reasons behind the restriction on using non-public members in TypeScript classes?

Consider the following scenario: class Trait { publicMethod() { this.privateMethod(); // do something more } private privateMethod() { // perform a useful action } } When attempting to implement it like this: cla ...

Experiencing migraines while integrating Firebase 9, Redux Toolkit, and Typescript in a React project. Encountering a peculiar issue where 'dispatch' is unexpectedly identified as type 'never'?

I am currently in the process of upgrading an old project to newer technologies, specifically focusing on Typescript, redux-toolkit, and Firebase v9 for modularity. While I have limited experience with Typescript and none with redux-toolkit, I have been us ...

Trying out cellRenderer in Angular's Ag Grid with Jest to validate values

I am facing an issue where I need to test the actual values displayed in a column of an Ag Grid table. These values are formatted using a cellRenderer based on specific conditions. In my Jest test, I have experimented with various approaches: Using fixtu ...

Typescript i18next does not meet the requirement of 'string | TemplateStringsArray NextJS'

While attempting to type an array of objects for translation with i18next, I encountered the following error message in the variable navItems when declaring i18next to iterate through the array Type 'NavItemProps[]' does not satisfy the constrain ...

Improving the process of class initialization in Angular 4 using TypeScript

Is there a more efficient method to initialize an inner class within an outer class in Angular 4? Suppose we have an outer class named ProductsModel that includes ProductsListModel. We need to send the ProductId string array as part of a server-side reque ...

Develop an Angular resolver that can be utilized across various scenarios

Im searching for a method to apply a single angular route resolve to all of my routes, each with different parameters: currently, my setup looks like this: { path: 'user/:any', component: UserprofileComponent, resolve ...

Resetting the forms as users navigate between different tabs on the mat

I am currently working on an Angular application and I would like to incorporate mat-tabs. However, I am facing an issue where every time I switch between tabs, the inputs in each tab are resetting to empty values. Here is the HTML code snippet: <mat- ...