30

I have this code in app-routing.module.ts, as per the new documentation in angular I went through the method still it's not working, throwing some errors I can't understand.

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule, Routes } from "@angular/router";
import { HomeComponent } from "./home/home.component";
import { AdminModule } from "./admin/admin.module";

const routes: Routes = [
  {
    path: 'admin',
    loadChildren: './admin/admin.module#AdminModule'
  },
  { path: '', redirectTo: '/home', pathMatch: 'full' },
  { path: 'home', component: HomeComponent },
];

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

It's throwing an error like this.

I have also tried the old way of loadchildren like this 'app/admin/admin.module#AdminModule'. But it's still not working.

core.js:1601 ERROR Error: Uncaught (in promise): TypeError: undefined is not a function
TypeError: undefined is not a function
    at Array.map (<anonymous>)
    at webpackAsyncContext ($_lazy_route_resource lazy namespace object:18)
    at SystemJsNgModuleLoader.push../node_modules/@angular/core/fesm5/core.js.SystemJsNgModuleLoader.loadAndCompile (core.js:5569)
    at SystemJsNgModuleLoader.push../node_modules/@angular/core/fesm5/core.js.SystemJsNgModuleLoader.load (core.js:5561)
    at RouterConfigLoader.push../node_modules/@angular/router/fesm5/router.js.RouterConfigLoader.loadModuleFactory (router.js:3294)
    at RouterConfigLoader.push../node_modules/@angular/router/fesm5/router.js.RouterConfigLoader.load (router.js:3282)
    at MergeMapSubscriber.project (router.js:1479)
    at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._tryNext (mergeMap.js:117)
    at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._next (mergeMap.js:107)
    at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:93)
    at Array.map (<anonymous>)
    at webpackAsyncContext ($_lazy_route_resource lazy namespace object:18)
    at SystemJsNgModuleLoader.push../node_modules/@angular/core/fesm5/core.js.SystemJsNgModuleLoader.loadAndCompile (core.js:5569)
    at SystemJsNgModuleLoader.push../node_modules/@angular/core/fesm5/core.js.SystemJsNgModuleLoader.load (core.js:5561)
    at RouterConfigLoader.push../node_modules/@angular/router/fesm5/router.js.RouterConfigLoader.loadModuleFactory (router.js:3294)
    at RouterConfigLoader.push../node_modules/@angular/router/fesm5/router.js.RouterConfigLoader.load (router.js:3282)
    at MergeMapSubscriber.project (router.js:1479)
    at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._tryNext (mergeMap.js:117)
    at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._next (mergeMap.js:107)
    at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:93)
    at resolvePromise (zone.js:814)
    at resolvePromise (zone.js:771)
    at zone.js:873
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
    at Object.onInvokeTask (core.js:4062)
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:420)
    at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:188)
    at drainMicroTaskQueue (zone.js:595)
    at ZoneTask.push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask [as invoke] (zone.js:500)
    at invokeTask (zone.js:1540)
3
  • please include the AdminModule, AFAIK lazy loading and router hasn't change a lot in Angular 6 Commented May 8, 2018 at 23:58
  • 1
    As one note, don't import the AdminModule module in your AppRoutingModule module. Do you see the new chunk being downloaded in your network tab in Chrome? Are you exporting your module in AdminModule? What do the routes look like for that module? Commented May 9, 2018 at 0:41
  • {path: 'admin', loadChildren:() => AdminModule } this is the solution to the problem Commented Jul 9, 2018 at 9:54

7 Answers 7

45

I had the same problem, the cause for me was that i was importing the lazy loaded module in my app module.

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

3 Comments

Same here. I had initially imported a feature module into my app module, then read about lazy loading and edited the routes but forgot to remove the import statements. The error message "undefined is not a function" was not helpful in the least.
Yes, lazy loaded modules should NOT be imported directly to the root module by their nature, haha.
I had the same issue, I imported directly to AppModule and also lazy-loaded the module, removing it from the AppModule imports fixed it.
9

This error is firing after some recompilation when ng serve is running, and after that, it shows always. Restarting ng serve - solved this problem.

Important!

Using loadChildren as function - is Not lazy loading:

{path: 'admin', loadChildren:() => AdminModule } //not lazy loading

cause you need to import the lazy module in the routing module.

For Lazy loading, you must send the path to the lazy module - as String!

{path: 'admin', loadChildren:'app/admin/admin.module#AdminModule'} // This is lazy loading

4 Comments

Don't forget to NOT important AdminModule to the AppModule since it should be loaded lazily.
@hastrb Your comment is not necessary because I already wrote it in the answer itself. read it again.
you're not right. There is no words about NOT importing it eagerly to the root module. It is not enough just to configure routing since the most part of developers add lazy modules to an array of important to the root module at first attempt before googling it.
@hastrb As I said, please read the answer again! see in line number 3 - 5: Important! Using loadChildren as a function - is Not lazy loading cause you need to import the lazy module in the routing module.
8

I have also faced the same problem while using Angular-7 and Angular CLI: 7.1.3 and tried to find some solution. It is solved for me by removing import statement of lazy loaded module from app.module file.

// remove this from app.module and app-routing.module file and it will work fine
import { AdminModule } from "./admin/admin.module";

My project configuration for reference

1 Comment

+1 This was exactly what I needed! I was importing the Modules — that I was lazy loading in app-routing.module — inside my app.module file! It seems unintuitive to remove these imports in app.module but it solved this issue immediately. I even had a previous code example to draw from and didn't catch this subtlety.
2

Both the syntax

{path: 'admin', loadChildren:() => AdminModule } //(Dynamic import using lambda expression)

and

{path: 'admin', loadChildren:'app/admin/admin.module#AdminModule'} // (Dynamic import using string literal) 

should ideally work with Angular 8+ using both AOT compiler and JIT compiler for lazy loading feature modules. However, string literal way of lazy loading the modules works the best in angular-cli version 6 & 7 (JIT and AOT compiler).

You might still be stuck with the 'TypeError' thrown when using string literals with JIT compiler (ng serve), while it works perfectly fine with AOT(ng serve --aot). The error 'TypeError: undefined is not a function' is quite misleading. Please find a reference to the Github issue that was raised concerning the same problem, for more details.

https://github.com/angular/angular-cli/issues/10582

Solution:

This error is actually caused due to the circular dependency of module injection which arises when you import the lazy load intended module in your parent module as mentioned in many many articles you might have come across. This may be done by you either unintentionally or intentionally when you want to use an exported component/ service of your lazy loaded module elsewhere.

The best way to solve this is by clearly differentiating shared components and building a module of shared components and re-using them i.e. moving the export components/ services of a lazy loaded module to a shared / common module.

Hope my answer helps you.

Comments

1

I faced the same issue solved it like:-

  1. run => ng serve --aot
  2. Removed the module from import of the root module because i was already using module in the routing in loadChildren section.

By using both of the ways i was able to run the application. So, you can try any one of these.

1 Comment

The first step is unnecessary. The most important thing to know is to NOT import lazy modules in your root module such as AppRoot. It should be loaded lazily, sometimes even never for some user cases.
1

We didn't need to import AdminModule to app.module

Remove this from app.module and app-routing.module file and it will work fine.

import { AdminModule } from "./admin/admin.module";

Comments

-6
{path: 'admin', loadChildren:() => AdminModule }

try this. this is the solution to the problem

3 Comments

While this does work, I am pretty certain this actually is no longer lazy loading, as getting this code to compile, you'd have to import AdminModule up front and have a hard reference to the module
This works, but this also disables lazy loading. In my opinion, not a valid answer to this question.
This solution doesn't have any sense. If you use this idea you lose lazy loading for this module...

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.