Issue with aliasing in tsconfig.app.json not functioning correctly in Angular 11

I'm facing a problem with aliasing after migrating my application to an Angular project.

None of my imports are functioning properly with the ""@app"" alias.

mainApp
│   package.json
│   tsconfig.json
│   angular.json    
│
└───projects
    │  
    └───my-project (The specific project requiring aliasing)
        │  tsconfig.app.json
        │ 
        └───src
             │  
             └───app

When I try to use:

import { MyService } from '@app/shared/services/my.service';

Error messages appear.

In my tsconfig.app.json:

{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "outDir": "../../out-tsc/app",
    "types": []
  },
  "files": [
    "src/main.ts",
    "src/polyfills.ts"
  ],
  "baseUrl": "./",
    "paths": {
      "@app/*": ["src/app/*"],
      "@env/*": ["environments/*"]
    },
  "include": [
    "src/**/*.d.ts"
  ]
}

I attempted

"@app/*": ["projects/my-project/src/app/*"],

I also made changes to my main tsconfig.json:

....
"baseUrl": "./",
  "paths": {
    "@app/*": ["src/app/*"],
    "@env/*": ["environments/*"]
  },
...

After these adjustments, I tried serving the project:

ng serve --project my-project

Answer №1

The reason for the problem was that my paths object was placed outside of the compilerOptions object, like this:

 "compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "@app/*": ["projects/my-/src/app/*"],
      "@env/*": ["environments/*"]
    },
    "outDir": "./dist/out-tsc",
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "module": "es2020",
    "lib": [
      "es2018",
      "dom"
    ]
  }

This configuration now works perfectly! Instead of placing it outside as shown in the incorrect code snippet:

 "compilerOptions": {
.......
},"paths": {
      "@app/*": ["projects/my-project/src/app/*"],
      "@env/*": ["environments/*"]
    }

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

Switching to Angular's routing, prioritize removal of the previous component

I'm currently using [routerLink]="['my-route']" in my Angular project. However, I've encountered an issue with the routing system where it renders the new component first and then removes the old one from the DOM. This is caus ...

Angular's array filter functionality allows you to narrow down an

I am working with an Array and aiming to filter it based on multiple criteria: primasSalud = [ { nombre: 'one', internacional: true, nacional: false, nacionalSinReembolso: false, nacionalClinicasAcotadas: false ...

I am experiencing an issue where the Javascript keydown event does not recognize the character "@" in the EDGE browser

Currently, I am developing a mentioning directive that displays a list of users when the user types in an input field (a div with contentEditable=true). The user can then insert the name of the desired user in a specific format. The list is supposed to be ...

Guide on passing LOCALE_ID from an observable within Angular 2

To localize the date pipe in Angular 2, you must supply the LOCALE_ID. I have developed a service called LocaleService that offers an locale$: Observable<string> which utilizes a BehaviorSubject<string>. I wish for this service to remain full ...

Troubleshooting AngularJS2 and npm in WebStorm and Chrome for Windows users

Having completed the official Hero tutorial for AngularJs2 using Visual Studio Code, I decided to switch my coding and debugging setup to WebStorm+Chrome on Windows 10. To achieve this transition, I took the following steps: Installed Chrome JetBrains ...

Stop the inheritance of static components in a feature module by protecting the router-outlet

I am in the process of dividing my app into multiple feature modules. Currently, I am using only the router-outlet inside a component within a feature module. However, this approach brings along all the static components such as the navbar and footer. How ...

Is it possible to conceal dom elements within an ng-template?

Utilizing ng-bootstrap, I am creating a Popover with HTML and bindings. However, the ng-template keeps getting recreated every time I click the button, causing a delay in the initialization of my component. Is there a way to hide the ng-template instead? ...

Manually initiating event broadcasts in Angular 5

After researching, I discovered a solution for implementing $broadcast and $on in Angular 5. It involves creating a custom service called broadcaster. I have two parallel components that need to listen for change events triggered by the parent component. ...

`Angular Image Upload: A Comprehensive Guide`

I'm currently facing a challenge while attempting to upload an image using Angular to a Google storage bucket. Interestingly, everything works perfectly with Postman, but I've hit a roadblock with Angular Typescript. Does anyone have any suggesti ...

Using InjectionToken within an Angular APP_INITIALIZER function

I am currently working on implementing an APP_INITIALIZER in my Angular 10 project. The factory function I'm using has multiple dependencies, one of which is an InjectionToken<string>. However, I have encountered an issue when trying to inject i ...

Error: Found an unconventional token "e" in JSON data at position 1 during parsing in JSON.parse function

Is there a way to retrieve a string value by calling an API in Angular? Here is my approach: Service file - public getRespondDashboardUrl(): Observable<any> { return this.http.get(`ref/RespondDashboardUrl`); } Component File respondDashboar ...

What is the best way to develop an Angular library with components in version 8 that can be seamlessly integrated into upcoming Angular versions such as 12, 13, and 14

Do I need to implement a new technique or setup in order to understand this? Can we use Angular elements as the only solution, or are there alternative approaches available? ...

Revise Swagger UI within toggle button switch

My project aims to showcase three distinct OpenApi definitions within a web application, enabling users to explore different API documentation. The concept involves implementing a toggle button group with three buttons at the top and the Swagger UI display ...

Angular error: Attempting to create a property on an empty string using Rxjs

Currently, I am working with Angular 11 and using rxjs. I am trying to filter my course collection to show only courses with the category "frontend," but I am encountering some errors. Here is my code: getPosts():Observable<ICourses[]> { return ...

Having issues with Angular 2/4 unable to read an object within the initForm() function

In the process of creating an edit form using Angular, I am facing a problem with understanding the lifecycle of the object retrieved from my backend server. After making a call to my service in ngOnInit(), I receive valid data. However, when I assign this ...

"Angular Google Maps module working perfectly on local environment but failing to render on live production site

My little application utilizes the official Angular Google Maps module. While it runs smoothly in my local setup, it simply displays a gray window in production. Upon inspecting the code, I can see that the map, marker, and info window are present, but not ...

Angular: Retrieve the source after navigating

Hello there, I am facing a simple problem. I have 2 components navigating to 1 component and in that one component, I need to distinguish which component the navigation came from so I can take appropriate action (such as refreshing a list). The issue is t ...

Discover the method to determine the total count of days in a given week number

I am developing a gantt chart feature that allows users to select a start date and an end date. The gantt chart should display the week numbers in accordance with the ISO standard. However, I have encountered two situations where either the start week numb ...

Angular 2 template can randomly display elements by shuffling the object of objects

I am working with a collection of objects that have the following structure: https://i.stack.imgur.com/ej63v.png To display all images in my template, I am using Object.keys Within the component: this.objectKeys = Object.keys; In the template: <ul ...

The term "Exports" has not been defined

I'm currently facing a challenge trying to develop an Angular application based on my initial app. The process is not as smooth as I had hoped. Here's the current setup: index.html <!DOCTYPE html> <html> <head> <base h ...