angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

lazy loading not working in Angular 6

Open cagline opened this issue 8 years ago • 44 comments

I'm getting this error when try to navigate to 'http://localhost:4200/dashboard' lazy loading route in angualr, angualr-cli 6.0.0

ERROR Error: Uncaught (in promise): Error: Cannot find module "app/dashboard/dashboard.module". Error: Cannot find module "app/dashboard/dashboard.module". at $_lazy_route_resource lazy namespace object:5

const routes: Routes = [
    {
        path: 'login',
        loadChildren: 'app/login/login.module#LoginModule',

    },
    {
        path: '',
        component: MasterComponent,
        children: [
            {
                path: 'dashboard',
                loadChildren: 'app/dashboard/dashboard.module#DashboardModule'
            }
        ],
    },
    {
        path: '**',
        redirectTo: 'login
    }
];


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

cagline avatar May 06 '18 07:05 cagline

@Steveiwonder Thanks a lot. Your solution worked for me.

vivekreddy avatar Aug 10 '18 13:08 vivekreddy

I have solve the issue by updating angular cli and other packages to latest version by checking it through ng udpate command

Name Version Command to update -------------------------------------------------------------------------------- @angular/cli 6.0.8 -> 6.1.5 ng update @angular/cli @angular/core 6.1.0 -> 6.1.4 ng update @angular/core @angular/material 6.4.2 -> 6.4.6 ng update @angular/material

Vino16491 avatar Aug 24 '18 15:08 Vino16491

Hi folks I try following it working. { path: 'customers', loadChildren: 'src/app/customers/customers.module#CustomersModule' }, { path: 'orders', loadChildren: 'src/app/orders/orders.module#OrdersModule' }

I try following code also, it working { path: 'customers', loadChildren: './customers/customers.module#CustomersModule' }, { path: 'orders', loadChildren: './orders/orders.module#OrdersModule' }

so question is which is better approach as feature prospective ?

phutaneVinayak avatar Sep 04 '18 07:09 phutaneVinayak

@Vino16491

didn't work for me :-1:

shiamalon avatar Sep 19 '18 14:09 shiamalon

I'm getting a similar error after upgrading from Angular 5 to 6:

Router Event: NavigationError platform-browser.js:211 NavigationError(id: 4, url: '/ping', error: TypeError: undefined is not a function) ERROR Error: Uncaught (in promise): TypeError: undefined is not a function TypeError: undefined is not a function

Utilizing this syntax has worked for me locally

Before (didn't work): { path: 'bing', loadChildren: './bing/bing.module#BingModule' }

After (does work): { path: 'bing', loadChildren: () => BingModule}

Though when I upload to Heroku I am getting the error again (I am using Express for deployment). The strange part is the old syntax works with Express.... what is going on?!

Angular CLI: 6.1.5 Angular: 6.1.6 Node: 8.11.4

daintym0sh avatar Sep 21 '18 20:09 daintym0sh

When upgrading from Angular 5 to Angular 6 or Angular 7, we had the following errors from lazily-loaded modules, after the upgrade:

Error: Uncaught (in promise): Error: Loading chunk default~[long module name with key suffix] failed.

at

core.js:12632

The solution was to convert ALL our routes from the format

{ path: 'xyz', loadChildren: 'pathtomodule#ModuleName' }

to the lambda expression format

{ path: 'xyz', loadChildren: () => ModuleName }

Peter2090 avatar Nov 03 '18 13:11 Peter2090

@Peter2090 did you run into a "Maximum call stack size exceeded" with the latter of the two approaches? I just started a new Angular 7 project and can't get routing to work with any of the normal (or abnormal) module approaches. Tried what you mentioned, along with the relative path approach, absolute path approach, and all three with and without importing the module in the App Module (I realize it shouldn't be there, figured I'd do my due diligence and test it though).

klst-speterson avatar Nov 12 '18 18:11 klst-speterson

No...the second approach fixed our 'Error: Loading chunk' errors. It sounds like you have a circular reference. Is there a circular reference in the module imports somehow?

Peter2090 avatar Nov 12 '18 18:11 Peter2090

I've been searching nonstop for a circular reference, but this is literally the first module/route. There's all of like 3 and half lines of code.

klst-speterson avatar Nov 12 '18 18:11 klst-speterson

All I can suggest is cutting everything out from your project apart from one module to lazy load, but you seem to have done that.

Peter2090 avatar Nov 12 '18 18:11 Peter2090

I'm getting a similar error after upgrading from Angular 5 to 6:

Router Event: NavigationError platform-browser.js:211 NavigationError(id: 4, url: '/ping', error: TypeError: undefined is not a function) ERROR Error: Uncaught (in promise): TypeError: undefined is not a function TypeError: undefined is not a function

Utilizing this syntax has worked for me locally

Before (didn't work): { path: 'bing', loadChildren: './bing/bing.module#BingModule' }

After (does work): { path: 'bing', loadChildren: () => BingModule}

Though when I upload to Heroku I am getting the error again (I am using Express for deployment). The strange part is the old syntax works with Express.... what is going on?!

Angular CLI: 6.1.5 Angular: 6.1.6 Node: 8.11.4

After many days trying to find a solution, this worked!!

benmartink avatar Jan 02 '19 12:01 benmartink

But sorry @benmartink if you do like this { path: 'bing', loadChildren: () => BingModule} the lazy module code is loaded in main bundle. The import break the lazy load beaviour (webpack add this in main chunk not in the lazy one)

@filipesilva Please can you clarify if we need the baseUrl in the sub tsconfig ? Thanks :)

gcirone avatar Jan 04 '19 16:01 gcirone

Hi @gcirone , yeah you right. I made an architecture where it loads the way you are saying. The only problem I had was that when upgraded angular and several modules related to it, it started to display the error 'Cannot find module "app/xyz/xyz.module". at $_lazy_route_resource lazy'. So, I tried several suggestions found on internet but no one helped until this one!

Btw, I needed to upgrade angular because I wanted to use Angular Materials Design.

benmartink avatar Jan 04 '19 17:01 benmartink

Hi, I have found that I have small bug inside HTML. I have found it during generation of translation files with command "ng xi18n". After fixing all compilation errors my lazy loading starts working. That error wasn't visible during normal work.

Przemyslaw-Rumin avatar Jan 11 '19 08:01 Przemyslaw-Rumin

I solved the issue by adding in the angular.json file the lazyModules property and map the same url in the router configuration.

I have to say thanks to Angular guys because now we can load the lazy module direct from the node_modules folder. LOVE IT 🥇

gcirone avatar Jan 12 '19 11:01 gcirone

@gcirone :100: Where were you when i faced the same problem. After lots of tries and waste of time I came to same solution. This is best solution available by @gcirone I hope there should be a direct channel to talk to angular guys who will say, STOP man you are doing this wrong. This is how we have created it and this is how you gonna use it.

parasharrajat avatar Jan 18 '19 14:01 parasharrajat

this will work './login/login.module#LoginModule' './dashboard/dashboard.module#DashboardModule'

Actually, I tried in this way. I'm facing an error

S2057 avatar Jan 25 '19 18:01 S2057

I wasted way too much time on this only to realize that since I'm using Webpack without the Angular CLI I had to add angular-router-loader to the configuration for it to work and generate the chunks.

https://www.npmjs.com/package/angular-router-loader

Hoping someone else that comes around here, or those that have resorted to using => may find this useful.

blindstuff avatar Mar 15 '19 00:03 blindstuff

I have faced a similar problem twice so far and have ended here both times. So I'm just gonna leave this here for the next time I stumble upon the same problem. I had a problem where the lazy loaded module was not found, but sometimes after changing any of the files which triggered rebuild, it started working. The problem was an unexported function inside app.module which was actually not used anywhere else (once it was a urlmatcher, and the other time a factory function used in a provider). Both times were solved by adding export before the function declaration.

izakgl avatar Mar 18 '19 13:03 izakgl