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

Vue3: The module './assets/logo.png' and its corresponding type declarations are not found

The project was created using the following command: vue create testtype3 Link to image: https://i.sstatic.net/vMuq0.png App.vue: <template> <img alt="Vue logo" src="./assets/logo.png"> <img :src="MyIcon" ...

Angular failing to recognize the && operator

I have a button that opens a dialog with the blockui feature. I am trying to close the dialog and set the blockui boolean variable to false in order to stop blocking the UI. However, in my HTML code (click)="blockedDialog=false && displayAddDialog=false", ...

How can Mui typescript be extended with a unique wrapper that includes a `component` property?

I recently created a unique wrapper component: import Box, { BoxProps } from "@mui/material/Box"; type CustomWrapperProps = { id: string } & BoxProps const CustomWrapper = (props: CustomWrapperProps) => { const {id, children, ...rest ...

How to convert typescript path aliases into relative paths for NPM deployment?

I am currently working on a typescript project that utilizes paths for imports. For instance: "paths": { "@example/*": ["./src/*"], } This allows the project to import files directly using statements like: import { foo } from "@example/boo/foo"; Whe ...

What causes the website to malfunction when I refresh the page?

I utilized a Fuse template to construct my angular project. However, upon reloading the page, I encountered broken website elements. The error message displayed is as follows: Server Error 404 - File or directory not found. The resource you are looking fo ...

"Encountering a TypeScript error when using React Query's useInfiniteQuery

I am currently utilizing the pokeApi in combination with axios to retrieve data import axios from 'axios' export const fetchPokemonData = async ({ pageParam = "https://pokeapi.co/api/v2/pokemon?offset=0&limit=20" }) => { try ...

Exchange a TypeScript data type with a different one within an object

I am currently working with the following type definitions: type Target = number | undefined; type MyObject = { some: string; properties: string; id: Target; } I am trying to find a generic solution to replace instances of Target with number ...

Problem with updating Cypress e2e tests following recent package upgrades

My current project involved updating all the packages to their latest versions. Prior to the update, all end-to-end tests were functioning correctly without any issues. After completing the update, the product itself compiles and runs as expected without ...

Extending an interface in TypeScript does not permit the overriding of properties

While working with Typescript, I encountered an issue where I couldn't make a property not required when overwriting it. I have defined two interfaces: interface IField { label: string; model: string; placeholder? ...

Angular 2 Material - Troubleshooting: Why does my input lose focus?

I've encountered a strange issue. Using Angular CLI, I integrated Material 2. Created inputs with ngfor and linked them to ngmodel. Everything was functioning correctly... Except, as I type in the input field, it deselects itself. This is the snipp ...

Transforming dynamic class based on state value from React to TypeScript

I'm trying to implement this React function in TypeScript, but I'm encountering errors. const ListItem = ({ text }) => { let [showMore, setShowMore] = useState(false); return ( <div className="item"> ...

Begin by executing the angular repository on your local machine

Is it possible to run the Angular repository found on GitHub at github.com/angular/angular locally through localhost? The README for the repository does not offer any guidance on running it locally. Running 'yarn' will build the site, but how do ...

What is the best way to declare a global TypeScript variable in a definition file to make it accessible for import?

My issue revolves around an external JS library that contains a global parameter: function Thing() { ... } ... var thing = new Thing(); I have a TypeScript definition file named thing.d.ts with the following content: declare var thing: ThingStatic; ex ...

Typescript struggling to comprehend the conditional rendering flow

I am facing an issue with the code snippet below: import * as React from 'react' type ComponentConfig = [true, {name: string}] | [false, null] const useComponent = (check: boolean): ComponentConfig => { if (check) { return [true, {name ...

The navigation function in Angular, this.router.navigate, is causing issues and

I've encountered a peculiar issue. There's a logout function that is activated whenever I receive a 401 response from any API I interact with. The function looks like this: constructor( private router: Router, ) {} logout(router1: Router ...

Deploying Angular 5 to Azure App Services is causing deployment issues

We are facing challenges with deploying an Angular 5.1.3 App to an Azure App Service. Our deployments are triggered by commits to a GitHub repo. Each deployment is taking several minutes and ultimately resulting in the error message below We are seeking a ...

ngx-scroll-event activated, yet remains elusive

After updating my project from Angular 7 to 8 smoothly, I proceeded with the update to Angular 9. Suddenly, the project was unable to find the required [email protected] package, resulting in a "module not found" error: Cannot find module 'ngx-sc ...

Combine objects by selecting attributes from multiple objects

declare type A = {type: 'TypeA', attr1: string} declare type B = {type: 'TypeB', attr2: string} declare type U = A | B When I use type X = Pick<U, 'type'>, I get: { type: 'TypeA' | 'TypeB' } But I a ...

The two-way binding does not connect the property and event halves to the same target

I am trying to create a two-way binding using reactive forms in Angular. I need to exchange data between the child component and the parent component seamlessly. This is the HTML code for my child component: <input type="text" #name class=&qu ...

What is the correct way to use Observables in Angular to send an array from a Parent component to a Child

Initially, the task was to send JSON data from the parent component to the child component. However, loading the data through an HTTP request in the ngOnInit event posed a challenge as the data wasn't being transmitted to the child component. Here is ...