32

I'm using angular cli AoT compilation. When I try to make a lazy load component following this tutorial, I got the error below:

ERROR Error: Uncaught (in promise): TypeError: __webpack_require__.e is not a function
TypeError: __webpack_require__.e is not a function
    at webpackAsyncContext (eval at ./src/$$_lazy_route_resource lazy recursive (main.bundle.js:13), <anonymous>:15:29)
    at SystemJsNgModuleLoader.loadAndCompile (core.js:6554)
    at SystemJsNgModuleLoader.load (core.js:6538)
    at RouterConfigLoader.loadModuleFactory (router.js:4543)
    at RouterConfigLoader.load (router.js:4523)
    at MergeMapSubscriber.eval [as project] (router.js:2015)
    at MergeMapSubscriber._tryNext (mergeMap.js:128)
    at MergeMapSubscriber._next (mergeMap.js:118)
    at MergeMapSubscriber.Subscriber.next (Subscriber.js:92)
    at ScalarObservable._subscribe (ScalarObservable.js:51)
    at webpackAsyncContext (eval at ./src/$$_lazy_route_resource lazy recursive (main.bundle.js:13), <anonymous>:15:29)
    at SystemJsNgModuleLoader.loadAndCompile (core.js:6554)
    at SystemJsNgModuleLoader.load (core.js:6538)
    at RouterConfigLoader.loadModuleFactory (router.js:4543)
    at RouterConfigLoader.load (router.js:4523)
    at MergeMapSubscriber.eval [as project] (router.js:2015)
    at MergeMapSubscriber._tryNext (mergeMap.js:128)
    at MergeMapSubscriber._next (mergeMap.js:118)
    at MergeMapSubscriber.Subscriber.next (Subscriber.js:92)
    at ScalarObservable._subscribe (ScalarObservable.js:51)
    at resolvePromise (zone.js:809)
    at resolvePromise (zone.js:775)
    at eval (zone.js:858)
    at ZoneDelegate.invokeTask (zone.js:421)
    at Object.onInvokeTask (core.js:4736)
    at ZoneDelegate.invokeTask (zone.js:420)
    at Zone.runTask (zone.js:188)
    at drainMicroTaskQueue (zone.js:595)
    at ZoneTask.invokeTask [as invoke] (zone.js:500)
    at invokeTask (zone.js:1517)

Here are part of my codes:

app-routing.module.ts

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

const routes: Routes = [
  { path: 'listes', loadChildren: 'app/component/list/list.module#ListModule'}
];

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

list-routing.module.ts

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

import { ListComponent } from './list.component';

const routes: Routes = [] = [
  { path: '', component: ListComponent }
];

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

list.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { ListRoutingModule } from './list-routing.module';

import { ListComponent } from './list.component';

@NgModule({
  imports: [
    CommonModule,
    ListRoutingModule
  ],
  declarations: [ ListComponent ]
})
export class ListModule { }

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { HeaderComponent } from './component/header/header.component';
import { FooterComponent } from './component/footer/footer.component';
import { AppRoutingModule } from './app-routing.module';
import { DetailModule } from './component/detail/detail.module';
import { HomeComponent } from './component/home/home.component';


@NgModule({
  declarations: [
    AppComponent,
    HeaderComponent,
    FooterComponent,
    HomeComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {}

I already report it as an Angular-cli Issue. You can find it here.

Is there anyone who experienced with the same issue and found a solution for this?

Related bug: Angular 5 with Angular cli non-lazy loading modules in the router (Not solved yet).

Proposed solution: https://github.com/gdi2290/angular-starter/issues/1936:

{ path: 'listes', loadChildren: () => ListModule }
// it doesn't do lazy loading

Important information:

Angular cli: 1.7.0
Angular: 5.2.0

My regards

9 Answers 9

60

I got the same issue. I solve it Just stopping the cli server and start it. Error is gone if you done your code correctly.

Sign up to request clarification or add additional context in comments.

3 Comments

Helped me. While CLI server was running I added dependency to a lazy loaded module (by mistake). After server reboot the error has changed and it turned out there was missing service injector.
didn't believe ur answer at first time but it worked like a charm.... thanks so much
This fixed it for me too... derp.
32

I cloned and reproduced the issue using your posted GitHub code. In order to fix, your @angular/cli global and devDependencies packages must be at 1.7.2

npm remove -g @angular/cli
npm install -g @angular/[email protected]
npm remove @angular/cli
npm add @angular/[email protected] --save-dev

Now the @angular/cli package in your devDependencies matches the global version and it is set to 1.7.2 where that issue is resolved.

1 Comment

thanks, I got this problem before and it works fine, after one week I got it again! is that normal?
11

There is an open bug on angular-cli 1.7.x: https://github.com/angular/angular-cli/issues/9488#issuecomment-368871510

Downgrade to 1.6.8 solve the issue for me.

Comments

11

I got the same issue.fix it by using

 {path:'listes' ,loadChildren: ()=>ListModule} not {path:'listes' ,loadChildren: 'app/component/list/list.module#ListModule'}

1 Comment

I don't think ListModule will be lazy loaded anymore because doing ()=>ListModule you'll have to touch the class so the module will be eagerly loaded. Please confirm, thank you.
5

Please see this comment on the 1.7.x bug. The issue seems to be importing the lazy loaded module into the AppModule. Removing that import fixed the issue for me: https://github.com/angular/angular-cli/issues/9488#issuecomment-374037802

Comments

0

As for me the problem was in importing ChildModule after AppRoutingModule in AppModule. Reordering them fixed my problem.

1 Comment

Thanks! It really helped, AppRoutingModule should be last in imports array.
0

According to me the problem is that you may be lazy loading more than one module on same routes in same routing file. I also faced similar problem and change the name of one of the route

Comments

0

I had the same problem and managed to solve it by adding my lazy loaded modules to my angular CLI config file (angular.json). See my answer here: Angular 5 lazy loading Error: Cannot find module

Comments

-4

In your app.module.ts, did you import ListModule ?

I faced same issue, and was able to fix it by removing lazy loaded modules from imports in app.module.ts

3 Comments

What I want to d is lazy loading. If it is not lazy loading, it works perfectly
Yeah that's why I ask you to remove ListModule from app.module.ts if you imported it. Dunno why you missread my answer
I never import ListModule into app.module.ts

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.