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

AOT build produces applications unable to lazy load modules

Open slmyers opened this issue 10 years ago • 54 comments

angular-cli: 1.0.0-beta.16
node: 6.0.0
os: linux x64

reproduction repo can be found here

The log given by the failure.

main.cfeed71….bundle.js:547 EXCEPTION: Uncaught (in promise): Error: Cannot find module 
'app/lazy/lazy.module.ngfactory'.

It seems that aot option can not build project that lazily loads modules. I would hope that one could use the patterns in the official documentation -- which I believe I'm doing -- and use the -- aot option to compile your app easily. I would appreciate any clarification as to whether this is an issue with the CLI or with the compiler. Thanks.

slmyers avatar Sep 30 '16 23:09 slmyers

@achimha all I can say is that we're working as fast as possible to get it fixed. It's our top priority atm really.

filipesilva avatar Nov 03 '16 18:11 filipesilva

@filipesilva can you please confirm, when fixed should it also help overcome the memory errors that we encounter when compiling large project without lazy loading? Such the one referenced here https://github.com/angular/angular/issues/12184

naveedahmed1 avatar Nov 03 '16 18:11 naveedahmed1

Thanks for the update @achimha. I'm sure this is no small issue to work on and look forward to being able to leverage both AoT and lazy loading in our production app

tony-fanbeat avatar Nov 03 '16 18:11 tony-fanbeat

Is there a way to leverage lazy-load routes with AoT today? I'm using @ngtools/webpack and lazy routes are not recognized unless I also use angular2-router-loader. In this case, components are not precompiled anyway...

unsafecode avatar Nov 09 '16 17:11 unsafecode

A big shout out to @hansl and @filipesilva for making this work in angular-cli 1.0.0-beta.20-4. I have our app lazy load with AOT now. You guys rock! I appreciate all the effort that goes into fixing the tooling so that angular devs can focus on building applications, not build infrastructure 👍

JohannesRudolph avatar Nov 17 '16 16:11 JohannesRudolph

I'm still getting AppModule is not an NgModule in beta.20-4 using ng build -prod -aot

ctrl-brk avatar Nov 20 '16 08:11 ctrl-brk

Me too (well, not AppModule but the lazy module name instead), dunno how to solve it

Dunos avatar Nov 20 '16 11:11 Dunos

Yes, same thing here MyLazyLoadedModule is not and NgModule when trying ng build --prod --aot Repo here: https://github.com/MrCroft/lazy - ignore all the "angular-cli beta.18" commits; package.json and angular-cli.json are updated accordingly (for angular-cli 1.0.0-beta.20-4, respectively Angular 2.2.1)

@angular: 2.2.1
angular-cli: 1.0.0-beta.20-4
node: 6.9.1
os: win32 x64

NgxDev avatar Nov 20 '16 13:11 NgxDev

I can confirm Lazyloading with AoT (ng build --aot --prod) is working properly in latest release of Angular-Cli

Those having issues could try creating routing module and add to imports of AppModule.


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

@NgModule({
    imports: [
        RouterModule.forRoot([
            { path: '', loadChildren: 'app/home/home.module#HomeModule' },
            { path: 'lazyLoadedFeature', loadChildren: 'app/lazyLoadedFeature/lazy-loaded-feature.module#LazyLoadedFeatureModule' },
        ]
        )
    ],
    exports: [
        RouterModule
    ]
})
export class AppRoutingModule { }

In Lazy loaded module define its routing as a separate module and import in the child module.

import { FeatureListComponent } from './feature-list.component';
import { FeatureDetailsComponent } from './feature-details.component';

@NgModule({
    imports: [
        RouterModule.forChild([
            {
                path: '', component: FeatureListComponent,
                children: [
                    { path: 'details/:id', component: FeatureDetailsComponent },
                ]
            }
        ])
    ],
    exports: [
        RouterModule
    ]
})
export class LazyLoadedFeatureRoutingModule { }

naveedahmed1 avatar Nov 20 '16 14:11 naveedahmed1

@naveedahmed1 I've tried your suggestion on 2 apps: a bigger app, with multiple lazy loaded modules... and in the repo I gave the link to as an example ( https://github.com/MrCroft/lazy ) First case (the bigger app): After having created routing modules, ng build --aot doesn't throw errors anymore during the build process. But when browsing through the app, as soon as I try to access a link that needs to load a lazy module, I get this thrown in my browser's console: EXCEPTION: Uncaught (in promise): Error: Cannot find module 'app/user/user.module.ngfactory'. Error: Cannot find module 'app/user/user.module.ngfactory'.

Second case (the repo I gave the link to): Very strange, I'm still getting the same error on build: LazyModule is not an NgModule... I've checked and double-checked, I can't find anything missing.

NgxDev avatar Nov 20 '16 20:11 NgxDev

Make sure you are using latest version of cli. Can you try generating a new project using latest version of cli, then copy code in your src folder to the new project's src folder, compile and check if it works.

naveedahmed1 avatar Nov 20 '16 22:11 naveedahmed1

@naveedahmed1 My bad... I didn't think there was any difference between previous version and the latest (except only under the hood, but didn't think there would be any in the actual files generated initially). I did an ng init now, overwrote all the files and then put back from history whatever needed to have my old app back. The simple one (the repo) worked right away! For the larger app - I also had to delete the entire node_modules folder, not just angular and angular-cli (like I did with the small test app). But it works now! Both work. Thank you!

NgxDev avatar Nov 20 '16 23:11 NgxDev

@MrCroft and @naveedahmed1, the key change was the

    "baseUrl": "",

inside the tsconfig.json. The packages updates + this line solve the LazyModule is not an NgModule error at build.

jvitor83 avatar Nov 21 '16 03:11 jvitor83

I can confirm that I've just upgraded to angular-cli 1.0.0-beta.21 and lazy loading is working with AOT <3 ! Big shout out for the team :) !!

maxime1992 avatar Nov 24 '16 13:11 maxime1992

I found a problem when running --prod without --aot : Lazy loaded modules are no longer lazy loaded. Am I the only one to have that problem ? https://github.com/angular/angular-cli/issues/3267

maxime1992 avatar Nov 24 '16 15:11 maxime1992

@maxime1992 my lazy loading works fine with ng serve --prod with beta.21. Unfortunately, I can only partially validate with --aot. It compiles and serves the first lazy route without issues :smile: Then I run into the first @Effect custom decorator which is stripped out due to #2799 :cry:

Theory of Constraints in action :wink:

tomwanzek avatar Nov 24 '16 22:11 tomwanzek

Hi @tomwanzek ! Today is your lucky day :) I had exactly the same problem and I made a fork (like 5h ago) of angular-cli that doesn't strip out decorators! Just take a look at the end of #2799 ;) I tried it and it's working fine for me. I also added a bundle size comparison and it's way better than I could imagine. Only +7kb on main bundle. I thought keeping decorators would have a huge impact on bundle size but it's not that bad actually. Plus, I'll give a try to rollup now that AOT is working well and hopefully I'll get even better results!

maxime1992 avatar Nov 24 '16 23:11 maxime1992

HTH, I set up a demo repo to summarize how to make lazy routes work with @ngtools/webpack

https://github.com/unsafecode/ng2-aot

unsafecode avatar Nov 27 '16 10:11 unsafecode

I am also getting the same error of:

Error: No module factory available for dependency type: ContextElementDependency

and also using lazy loaded modules... any ideas?

angular-cli: 1.0.0-beta.26
node: 6.9.4
os: win32 x64
@angular/common: 2.4.5
@angular/compiler: 2.4.5
@angular/core: 2.4.5
@angular/forms: 2.4.5
@angular/http: 2.4.5
@angular/language-service: 2.4.5
@angular/platform-browser: 2.4.5
@angular/platform-browser-dynamic: 2.4.5
@angular/router: 3.2.1
@angular/compiler-cli: 2.4.5

Regards,

Checkout the Ultimate Angular 2 Boorstrap App: @ http://ng2.javascriptninja.io Source@ https://github.com/born2net/ng2Boilerplate

born2net avatar Jan 27 '17 03:01 born2net

@born2net I haven't seen that ContextElementDependency error in a few months now, I didn't think we had it anymore.

Can you open a new issue with a repo I can debug in?

filipesilva avatar Jan 27 '17 12:01 filipesilva