Skip to content

Instantly share code, notes, and snippets.

@vades
Last active May 22, 2019 12:49
Show Gist options
  • Save vades/ceb5f8db7f23ffa0ac9760fdf968f6e6 to your computer and use it in GitHub Desktop.
Save vades/ceb5f8db7f23ffa0ac9760fdf968f6e6 to your computer and use it in GitHub Desktop.
Use root paths for module imports in Angular

Use root paths for module imports

Add your application source via paths in tsconfig.json

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

Use root path in project

Extend tsconfig.app.json baseUrl in projects/<projectname>/tsconfig.app.json

{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "outDir": "../../out-tsc/app",
    "types": [],
    "baseUrl": "src",
  },
  "exclude": [
    "test.ts",
    "**/*.spec.ts"
  ]
}

Usage

import { environment } from '@env/environment';
import { ApiService } from '@app/core/services/http/api.service';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment