Nx: [tslint] deep imports into libraries are forbidden (nx-enforce-module-boundaries)

Created on 1 Feb 2018  路  6Comments  路  Source: nrwl/nx

I generated a new library in a new workspace and tried to import it into my app.

I get the above linting error when trying to import the lib into an app

import { MaterialModule } from '@myproject/material/src/material.module';

Any ideas what Im doing wrong

Most helpful comment

@aire-con-gas -

apps-import-libs

  • apps can never import other apps
  • libs can never import apps
  • apps can only import libs
  • libs can import libs

All 6 comments

What is reason, how it shall be in correct way? I have same problem, and in nx-example is the same problem

Still getting this issue with the following line:

import { DatabaseService } from '@my-products/database/src/database.service';

Any ideas?

As _always_ happens when I ask people questions, I found the answer myself. I just added

export * from './src/database.service';

to my index.ts of the lib. and updated the import:

import { DatabaseService } from '@my-products/database;

@jdk339 I'm finding my linter still complaining about import of apps are forbidden following your advice. I also updated the angular.json file and set the projectType to library but to no avail. Any thoughts?

@here - Here is a brief explanation:

This issue happens when you are using (aka importing) an external library into some source module and the import path by-passes the barrel (index.ts); by using /src/ or /src/lib/ as part of the import path.

The goal of the barrel is to define the public API of the library. Anything else is non-public, considered private/hidden and should NOT be directly accessed.

For libraries with source that wants to import files from inside the same lib, you should always use relative path imports.

Consider this:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { StoreModule } from '@ngrx/store';
import { EffectsModule } from '@ngrx/effects';
import { ticketsStateModelReducer } from './+state/tickets-state-model.reducer';
import { ticketsStateModelInitialState } from './+state/tickets-state-model.selectors';
import { TicketsStateModelEffects } from './+state/tickets-state-model.effects';

@NgModule({
  imports: [
    CommonModule,
    StoreModule.forFeature('ticketsStateModel', ticketsStateModelReducer, {
      initialState: ticketsStateModelInitialState
    }),
    EffectsModule.forFeature([TicketsStateModelEffects])
  ],
  providers: [TicketsStateModelEffects]
})
export class TicketsStateModule {}

Here we have two types of imports:

  • the public APIs for @ngrx/effects is used to import EffectsModule
  • also uses a relative import for ./+state/tickets-state-model.effects to load the nests TicketsStateModelEffects.

Additionally, while TicketsStateModule is exported as part of the public api for this library, the TicketsStateModelEffects is not made public and is considered a private internal module.

@aire-con-gas -

apps-import-libs

  • apps can never import other apps
  • libs can never import apps
  • apps can only import libs
  • libs can import libs
Was this page helpful?
0 / 5 - 0 ratings