Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular CLI ERROR in Cannot read property 'loadChildren' of null

I'm converting a project to use angular cli and everything is working (once it's build) but i've got a weird behaviour during build.

with ng serve I always get this error the first time I try: ERROR in Cannot read property 'loadChildren' of null

with ng build throws the same error

but if I use ng build --watch and after the first build fails and I edit a file to trigger the build again it will succeed. I've got also the same behaviour with ng build -prod --watch

Any ideas how to get the build right the first time?

please note that I do not have any child routes/modules in my project and I don't have any other output to see what is causing this.

Update: tested with child routes and I still get the same behaviour

Update: downgraded @angular libs to 2.4.0 from 4.0.0 and I still get the exact same behaviour, but with a different error message;

ERROR in AppModule is not an NgModule

like image 791
jperez Avatar asked Mar 30 '17 09:03

jperez


People also ask

What is LoadChildren in Angular?

Use LoadChildren:For lazy loading. Using this property will optimize your application's performance by only loading the nested route subtree when a user navigates to a particular URL that matches the current route path. It helps in keeping the nested routes table separate.

Why use lazy loading in Angular?

Lazy loading is an approach to limit the modules that are loaded to the ones that the user currently needs. This can improve your application's performance and reduce the initial bundle size. By default, Angular uses eager loading to load modules.

How lazyloading works in Angular?

Lazy loading is the process of loading components, modules, or other assets of a website as they're required. Since Angular creates a SPA (Single Page Application), all of its components are loaded at once. This means that a lot of unnecessary libraries or modules might be loaded as well.

How to register Router in Angular?

Register Router and Routes link To use the Router , you must first register the RouterModule from the @angular/router package. Define an array of routes, appRoutes , and pass them to the RouterModule. forRoot() method. The RouterModule.


3 Answers

I also ran into a similar issue with the Angular CLI v1.6.

In my case I was not using .concat() or any other kind of dynamic manipulation of the router definitions.

Rather I had a function in a data property of a route which was an anonymous arrow function. Changing this to a named exported function solved the issue for me.

Before:

 {
    path: ':id',
    component: ProductDetailComponent,
    data: {
        breadcrumb: (data: any, params: any) => {
            let id = params['id'];
            return id === 'create' ? 'New Product' : data.product.ShortDescription;
        }
    }
}

After:

{
    path: ':id',
    component: ProductDetailComponent,
    data: { breadcrumb: getBreadcrumb }
}

export function getBreadcrumb(data: any, params: any): string {
    let id = params['id'];
    return id === 'create' ? 'New Product' : data.product.ShortDescription;
}
like image 155
Michael Bromley Avatar answered Oct 22 '22 18:10

Michael Bromley


In my case, this answer to a related Github issue fixed my error.

In case it gets deleted, the fix was changing this:

export const ROUTES = MY_ROUTES.map(TO_ANGULAR_ROUTE)

to this:

export const ROUTES = [];
ROUTES.push(...MY_ROUTES.map(TO_ANGULAR_ROUTE));
like image 10
Lennart Hoffmann Avatar answered Oct 22 '22 17:10

Lennart Hoffmann


I got this error due to double comma in routing as below

enter image description here

like image 10
Varun Kumar Avatar answered Oct 22 '22 18:10

Varun Kumar