angular-cli: Cannot read property 'loadChildren' of undefined

Please provide us with the following information:

OS?

Windows 10

Versions.

@angular/cli: 1.0.0-beta.30 node: 6.9.1 os: win32 x64 @angular/common: 2.4.7 @angular/compiler: 2.4.7 @angular/core: 2.4.7 @angular/forms: 2.4.7 @angular/http: 2.4.7 @angular/platform-browser: 2.4.7 @angular/platform-browser-dynamic: 2.4.7 @angular/router: 3.4.7 @angular/cli: 1.0.0-beta.30 @angular/compiler-cli: 2.4.7

Repro steps.

feature.module.ts Using @nglibs/i18n-router to provide route translations for feature modules (lazy loaded).

import { I18NRouterModule } from '@nglibs/i18n-router';

@NgModule({
  imports: [
    CommonModule,
    // RouterModule.forChild(routes)
    I18NRouterModule.forChild(routes, 'about')
  ],
  declarations: [
    AboutComponent,
    AboutUsComponent,
    AboutBananaComponent,
    AboutApplePearComponent
  ]
})

The whole solution is located on github: angular-cli branch of @nglibs/example-app.

The log given by the failure.

Can’t talk about a stack trace, but hope it will be helpful:

$ ng serve
** NG Live Development Server is running on http://localhost:4200. **
Hash: ddefd75abb9ca8b82bd4                                                      Time: 18958ms
chunk    {0} polyfills.bundle.js, polyfills.bundle.map (polyfills) 222 kB {4} [initial] [rendered]
chunk    {1} main.bundle.js, main.bundle.map (main) 8.61 kB {3} [initial] [rendered]
chunk    {2} styles.bundle.js, styles.bundle.map (styles) 10 kB {4} [initial] [rendered]
chunk    {3} vendor.bundle.js, vendor.bundle.map (vendor) 3.49 MB [initial] [rendered]
chunk    {4} inline.bundle.js, inline.bundle.map (inline) 0 bytes [entry] [rendered]

ERROR in Cannot read property 'loadChildren' of undefined
webpack: Failed to compile.

Mention any other details that might be useful.

@nglibs/i18n-router does not work with @angular-cli (yet), and giving the following error during [AoT compilation]:

ERROR in Cannot read property 'loadChildren' of undefined

I suppose @angular-cli uses @ngtools/webpack for [AoT compilation], expecting RouterModule.forChild(...) to resolve lazy-loaded modules (with loadChildren), which is replaced by I18NRouterModule.forChild(...) - providing routes for feature modules instead.

Here’s the piece of code from i18n-router module line 72, which provides child routes.

                {
                    provide: ROUTES,
                    useFactory: (provideChildRoutes),
                    deps: [I18NRouterService, RAW_ROUTES, MODULE_KEY],
                    multi: true
                },
                {
                    provide: ANALYZE_FOR_ENTRY_COMPONENTS,
                    useValue: routes,
                    multi: true
                }

It provides ROUTES and ANALYZE_FOR_ENTRY_COMPONENTS to Angular the same way that the router module does (see below):

However, angular-cli seems insisting ignoring the provided child routes and firing errors.

To resolve this issue temporarily, I switched using [ng-router-loader]. Hence @angular-cli doesn’t allow modifying the webpack configuration, I need to manually configure build tools (dev/prod sever, task runners, webpack, etc).

I think loadChildren should be able to recognize routes provided with ROUTES and ANALYZE_FOR_ENTRY_COMPONENTS.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 6
  • Comments: 21 (2 by maintainers)

Most upvoted comments

I had the error. Found the cause in my case, want to share
in the TS app-routes module (of angular 5 project) I made a syntax error :

const routes: Routes = [ { path: ‘’, redirectTo:‘/’, pathMatch: ‘full’ } , { path: ‘a’, component : aComponent } , { path: ‘a/detail/:id’, component: aDetailComponent }, , { path: ‘b’, component : bComponent } , { path: ‘b/detail/:id’, component : bDetailComponent } ];

The error is the

,

at the end of the 4th line, which delivers an empty element in the Routes array, which delivers the loadChildren error.

I had the same error but yet another cause. I was dynamically assigning a key to the routes object:

const routes: Routes = [{
  path: '',
  component: SomeComponent,
  children: [{
    path: 'blah',
    component: BlahCreateComponent,
    resolve: {
      [SOME_CONSTANT]: SomeResolver, // <-- you can't do this
    },
  }],
}]

I was only able to track down the issue by doing ng build --prod --aot which gave me an more descriptive error.

I had the same error but yet another cause. I was dynamically assigning a key to the routes object:

const routes: Routes = [{
  path: '',
  component: SomeComponent,
  children: [{
    path: 'blah',
    component: BlahCreateComponent,
    resolve: {
      [SOME_CONSTANT]: SomeResolver, // <-- you can't do this
    },
  }],
}]

I was only able to track down the issue by doing ng build --prod --aot which gave me an more descriptive error.

Sure, in my case this did the trick:

const routes: Routes = []; routes.push(...links.map(link => <Route> { path: link.url, component: link.component }));

@thiagofrancisquete as @korenb mentioned, it seems like you can’t dynamically set the key of the object (using that syntax with the square braces [SOME_KEY]. Instead, you need to directly define the key.

As a concrete example, assume I was trying to assing the dynamic key like this

const SOME_CONSTANT = 'theKey'
...
    resolve: {
      [SOME_CONSTANT]: SomeResolver,
    },
...

…to fix it, you need to do this:

...
    resolve: {
      theKey: SomeResolver,
    },
...

run angular using aot option to get a full descriptive error ng s --aot

I came to this issue with a similar problem and actually found my fix here.

The problem I was having was I did not have the export on my function as soon as I added export to it I could build again. Sorry if this isn’t specifically related to the OP but thought it may help someone.

I had faced same issue. In My case I had not included my Route resolver in provider property of NgModule. After declaring in provider this issue get fixed.