Skip to content

Improve semantics of error message for incorrectly imported lazy loaded FeatureModule in AppModule #14065

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
danieldanielecki opened this issue Apr 3, 2019 · 4 comments

Comments

@danieldanielecki
Copy link

danieldanielecki commented Apr 3, 2019

🐞 Bug report

✍️ Description

Lazy routing is so often used a technique, but we still don't have semantic enough error message when a module to lazy route is included in imports of AppModule. Removing this import makes lazy loading implemented correctly, but the error which is being thrown during the import is not correct at all. Similar topics:

🔬 Minimal Reproduction

Step 1. Generate a new component to lazy load, here it's ng g c about-us.
Step 2. Create its module, here it's about-us.module.ts.
Step 3. Add RouterModule.forChild in about-us.module.ts, sample code below:

import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { AboutUsComponent } from './about-us/about-us.component';

@NgModule({
  declarations: [AboutUsComponent],
  imports: [RouterModule.forChild([{ path: '', component: AboutUsComponent }])]
})
export class AboutUsModule {}

Step 4. Add AboutUsModule (FeatureModule) to AppModule, AboutUsModule we want to lazy load, like that (code for app.module.ts):

AboutUsModule.

import { AppComponent } from './app.component';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { NgModule } from '@angular/core';
import { AboutUsModule } from '@app/about-us';

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    AboutUsModule
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

Step 5. Create app-routing.module.ts to define paths, for about-us it would be like that:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

const routes: Routes = [
  {
    path: 'about-us',
    loadChildren: '@ditectrev-libs/about-us/src/lib/about-us.module#AboutUsModule'
  }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class RoutingModule {}

Step 6. Add to app.component.html, below:

<router-outlet></router-outlet>

Step 7. Run ng serve, and navigate to localhost:4200/about-us, you should see an error like in Exception or Error section below.

🔥 Exception or Error

core.js:15713 ERROR Error: Uncaught (in promise): TypeError: undefined is not a function
TypeError: undefined is not a function

🌍 Your Environment


Angular CLI: 7.2.4
Node: 10.15.0
OS: win32 x64
Angular: 7.2.4
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.13.1
@angular-devkit/build-angular     0.13.1
@angular-devkit/build-optimizer   0.13.1
@angular-devkit/build-webpack     0.13.1
@angular-devkit/core              7.3.1
@angular-devkit/schematics        7.2.4
@angular/cdk                      7.3.2
@angular/flex-layout              7.0.0-beta.23
@angular/material                 7.3.2
@ngtools/webpack                  7.3.1
@schematics/angular               7.2.4
@schematics/update                0.12.4
rxjs                              6.3.3
typescript                        3.2.4
webpack                           4.29.0

Describe the solution you'd like to have

Throw an error something like "AboutUsModule is incorrectly imported into AppModule, because it supposed to be lazy loaded."

@ngbot ngbot bot added this to the needsTriage milestone Apr 3, 2019
@filipesilva filipesilva added the feature Issue that requests a new feature label Jun 13, 2019
@ngbot ngbot bot modified the milestones: needsTriage, Backlog Jun 13, 2019
@filipesilva filipesilva added freq3: high severity3: broken type: bug/fix and removed feature Issue that requests a new feature labels Jun 13, 2019
@filipesilva
Copy link
Contributor

I agree with you. This is super common. We could do better informing users about this pitfall.

@filipesilva filipesilva modified the milestones: Backlog, 8.1.0 Jun 13, 2019
@alan-agius4
Copy link
Collaborator

Related to #6373 (comment)

@clydin clydin modified the milestones: 8.1.x, Backlog Feb 7, 2020
@alan-agius4
Copy link
Collaborator

Thanks for reporting this issue. This issue is now obsolete due to changes in the recent releases. Please update to the most recent Angular CLI version.

If the problem persists after upgrading, please open a new issue, provide a simple repository reproducing the problem, and describe the difference between the expected and current behavior.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Jun 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants