Tutorial

Reference Angular Imports Absolutely for Easier Development

Draft updated on Invalid Date
Default avatar

By Chris on Code

Reference Angular Imports Absolutely for Easier Development

This tutorial is out of date and no longer maintained.

Introduction

Working with Angular has been a delight. Working with TypeScript, Observables, and the CLI have been great tools for development.

Referencing Imports Relatively

One piece of feedback I’ve noticed when working with Angular is when importing components and classes around your application.

Take the following for example:

import { HeaderComponent } from './components/header/header.component';
import { FooterComponent } from './components/footer/footer.component';
import { GifService } from '../core/services/gif.service';

All the imports here are referenced relatively. It can be a hassle to remember how many folders to jump into and out of.

If you move your files around, you’ll have to update all your import paths.

Let’s look at how we can reference imports absolutely so that TypeScript always looks at the root /src folder when finding items.

Our goal for this will be to reference things like so:

import { HeaderComponent } from '@app/components/header/header.component';
import { FooterComponent } from '@app/components/footer/footer.component';
import { GifService } from '@app/core/services/gif.service';

This is similar to how Angular imports are referenced using @angular like @angular/core or @angular/router.

Setting Up Absolute Paths

Since TypeScript is what is in charge of transpiling our Angular apps, we’ll make sure to configure our paths in tsconfig.json.

In the tsconfig.json, we’ll do two things by using two of the compiler options:

  • baseUrl: Set the base folder as /src
  • paths: Tell TypeScript to look for @app in the /src/app folder

baseUrl will be the base directory that is used to resolve non-relative module names. paths is an array of mapping entries for module names to locations relative to the baseUrl.

Here’s the original tsconfig.json that comes with a new Angular CLI install. We’ll add our two lines to compilerOptions.

{
  "compileOnSave": false,
  "compilerOptions": {
   ...

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

With that in our tsconfig.json, we can now reference items absolutely!

import { HeaderComponent } from '@app/components/header/header.component';
import { FooterComponent } from '@app/components/footer/footer.component';
import { GifService } from '@app/core/services/gif.service';

This is great because we can now move our files around and not have to worry about updating paths everywhere.

A Little Fun

For my projects, I like to update the @app to be something more personalized. Scotch projects use @scotch and other projects use @batcave.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Chris on Code

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel