Having trouble figuring out the tsconfig paths issue

In my Angular 6 project, I am using TypeScript version "2.7.2" and the tsconfig.json file looks like this:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./src",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  },
  "paths":{
    "@services/*": ["app/services/*"] 
  }
 }

I attempted to import a service using the path provided above:

 import { AppService } from '@services/app.service';

However, when I run ng serve, I encounter the following error:

ERROR in src/app/app.module.ts(20,26): error TS2307: Cannot find module '@services/app.service'.

P.S.- Importing the service without the configured path like this:

import { AppService } from './services/app.service';

works perfectly fine.

Answer №1

Configure the <code>paths setting within the compilerOptions

{
  "compileOnSave": true,
  "compilerOptions": {
    "baseUrl": "./src",
    "outDir": "./dist/out-tsc",
    "sourceMap": false,
    "declaration": true,
    "moduleResolution": "node",
    "emitDecoratorMetadata": false,
    "experimentalDecorators": false,
    "target": "es6",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2019",
      "dom"
    ],
    "paths":{
      "@models/*": ["app/models/*"] 
    }
  },
 }

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

React is struggling to locate the specified module

Once I've set up my react project using npx create-react-app called my-app, I proceed to run npm start only to encounter the error shown in the image below. Running node version: 12.16.1 with npm version: 6.13.4 View the error message her ...

Using *ngIf with local variables in Angular 2: How to prevent binding to only the first element of an array

Check out the code on Plunker: https://plnkr.co/edit/te67zdtkVkNCUzEtW5XO?p=preview. Look at app.ts for details. Scenario: In this setup, the parent component holds an array and displays one element from it. The ChildComponent is a list that shows all el ...

What could be causing my TypeScript code to not be recognized as CommonJS?

I rely on a dependency that is transpiled to ES6. My goal is to leverage ES2019 features in my own code. Ultimately, I aim to output ES6. This is how I set up my tsconfig { "compilerOptions": { "module": "CommonJS" ...

Exploring the implementation of enums in a separate file with Angular Ahead-Of-Time compilation

Converting a well-functioning JIT-based scenario angular-based app (angular-cli) to an AOT-based scenario. The AOT-build is successfully running. Upon opening the resulting webpage, I am encountering errors stating 'Cannot read property 'none&ap ...

Unable to utilize external JavaScript files in Angular 8

I've been working on integrating an HTML template into my Angular project and for the most part, everything is going smoothly. However, I've encountered an issue where my JS plugins are not loading properly. I have double-checked the file paths i ...

some elements of the procedural mesh are not being rendered correctly in ThreeJS

After completing some testing with a model I designed for threeJS, I am now bundling shapes into an npm package and exporting it to an Angular project. The interesting thing is that the HTML version on the left almost mirrors the Angular version on the rig ...

Leveraging JSON.stringify alongside getter/setter in TypeScript

In my TypeScript code, I am utilizing getter/setter accessors. To differentiate between variables and methods with the same name, I have adopted the convention of prefixing the variable with a lower dash, as shown in many examples: private _major: number; ...

In a functional component in Typescript, what data type should be used for a parameter that accepts an array of objects?

const FormData = ({ dataArray }: object[]): JSX.Element => { console.log("Array of Objects",dataArray) //This is a large form component.... }); The dataArray contains multiple objects, how can I specify a specific type for these components ...

Angular 2 NG Dynamic Grid Columns

Can a grid be created where column widths are not fixed in pixels? This would particularly come in handy when paired with sizeColumnsToFit(). Take, for instance, the scenario in which the ID and Name columns should adjust to content size while the Descrip ...

The art of connecting with Angular 2 router and Components

Here are the elements I have: <app-scrollable-area (scrolledDown)="..." class="scrollable-y"> <router-outlet></router-outlet> </app-scrollable-area> I'm wondering how to communicate this event (scrolledDown) to inside ...

Utilize a jest mock object and pass it as a method parameter to achieve seamless testing

I am attempting to create a mock object (ColumnApi from ag-grid) using jest and then pass it as a parameter to a function that calls the "getAllColumns" method from ColumnApi. I am not concerned with how the "getAllColumns" method functions, but I want it ...

Unable to Sort Angular Material Data Table

Struggling to organize my data as the sorting functionality is not behaving as expected. Unlike the typical examples that use a local array of values, my situation involves an API call that returns an array of objects, with each object containing a 'n ...

Diving into Angular Typescript: Understanding the [object Object] in HTML

One of my todos requires the following input: todo.ts: import { TodoTag } from "./todo-tag-custom"; export class Todo { ... tags: TodoTag[]; } todo.html <tr *ngFor="let todo of todosFiltered()"> <td>{{todo.tags | json ...

An internal issue has occurred: RangeError: The call stack limit has been exceeded in Next.js React

Recently, I've encountered the error message "⨯ Internal error: RangeError: Maximum call stack size exceeded" and I'm struggling to pinpoint the root cause of this issue. My goal is to have the functionality where clicking the "Add to Cart" but ...

Element without style

In my app, I have implemented numerous material design components, but there are two input elements for which I would like to customize the style and remove the default material design look. Is there a way to eliminate the CSS styling from an <input ma ...

The addControl function inside a for loop and async function is not properly assigning a value to the form

My goal is to iterate through an array, make a HTTP request, retrieve another array from it, and assign the selected object from the fetched array to a newly added Form Control in the form. This is how I approached it: for (let i = 0; i < typeaheadFiel ...

Angular 7's masonry overlay technique

I decided to implement a masonry design in my Angular application using the ngx-masonry package found here: https://www.npmjs.com/package/ngx-masonry However, I encountered an issue where some items are overlapping, as shown in this image: https://i.sstat ...

What is the process for defining a local variable within an ngFor loop?

Is there a way to capture a snapshot of a class within *ngFor and store it in a local variable for efficiency? For instance, using the formula timer.remaining = timer.expires - Date.now() <ion-card class="timer" *ngFor="let timer of timers"> &l ...

Ways to ensure Angular stays current with live Node.js information?

Looking for a seamless way to notify my Angular frontend whenever a function is triggered in my Node.js backend. I want to avoid repeatedly hitting the backend with timers and instead establish a constant subscription. Any ideas on achieving this without ...

The return value depends on the type of function argument passed

I am currently developing a type-safe JSON:API specification parser for handling API responses that can either contain a single object or an array of objects (). For example, when making a request like GET /article: { data: { type: 'article&ap ...