1

Let say I have following component:

@Component({})
export class ServerComponent {

}

When typing @Component, Visual Studio Code suggest me to auto import @angular/core module. I accepted and get this import:

import { Component } from "../../../node_modules/@angular/core";

When I watch online courses, I see when the mentor perfomed the same actions he got much shorter path:

import { Component } from "@angular/core";

My app work OK for both paths, but I want to know how this happen, just for curious.

Questions:

  • Why my import path is different?
  • Is there any config that control this behavior?
  • Does this effect any behavior of my app?

My enviroment:

  • Windows 10 Pro x64
  • Visual Studio Code 1.25.1
  • NPM 5.6.0
  • Node 8.11.3
  • Angular CLI: 6.0.8 (npm install -g @angular/cli)
  • Angular 6.0.9

Mentor's environment:

  • Mac OSX
  • Visual Studio Code
  • Angular CLI 6.0.0 (npm install -g @angular/cli)
Cœur
  • 37,241
  • 25
  • 195
  • 267
NoName
  • 7,940
  • 13
  • 56
  • 108

2 Answers2

1

Take a look at the tsconfig.json in your main path.

There is (or can be) shortcuts defined for the imports. It looks like

"paths": {
      "@modules/*": ["app/modules/*"],
      "@core/*": ["app/core/*"],
      "@shared/*": ["app/shared/*"]
    }

With that everything that is in app/modules/MyModule/SomeComponent could be imported as @modules/MyModule/SomeComponent

warm regards

JanRecker
  • 1,787
  • 13
  • 17
0

Answers

  1. This is just absolute path difference the way visual studio works is it has the node_modules under the project folder added to your workspace ,so it will suggest you absolute path for the node_modules ,in your case @angular/core. In the tutorials you see or generally we just provide the @angular/core import because at runtime angular complier will resolve the path to absolute path ie..,'../../../node_modules/@angular/core'.
  2. There is no config control over to explicitly define this ,its rather the angular compiler behavior or the way angular ecosystem is built because post compilation all he modules gets invoked and injected on the main component.
  3. No it wont affect any behavior of your app.
Vaibhav
  • 1,481
  • 13
  • 17