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

###update

I think the LazyModule may not have been compiled, because there is a 0.map but no 0.chunk.js in the dist folder.

lazy_no_chunk

slmyers avatar Oct 02 '16 02:10 slmyers

#2112 seems aot process does not polished. I issue No module factory available for dependency type: ContextElementDependency for lazyRoutes dependencies

delagen avatar Oct 04 '16 07:10 delagen

I have this same issue as well. I dont have a repo to show yet but I get the same error as above. I ran ng build --prod --aot. Also one thing to note my outDir is set to dist/build instead of the default path.

angular-cli: 1.0.0-beta.16
node: 6.4.0
os: win32 x64
main.166efb8….bundle.js:1494 Unhandled Promise rejection: Cannot find module 'app/account/account.module.ngfactory'. ; Zone: angular ; Task: Promise.then ; Value: Error: Cannot find module 'app/account/account.module.ngfactory'.(…) Error: Cannot find module 'app/account/account.module.ngfactory'.

coryrylan avatar Oct 05 '16 14:10 coryrylan

I have the same problem and also don't see compiled code for the lazy modules, only the .ngfactory.ts file.

achimha avatar Oct 06 '16 11:10 achimha

In beta.16 the --aot flag was still experimental. Can you repro in beta.17?

filipesilva avatar Oct 09 '16 22:10 filipesilva

@filipesilva the same error. No changes in the main function in beta16->beta17 upgrade as I specified in https://github.com/angular/angular-cli/issues/2112#issuecomment-252343829 You can get source from @tianyangj comment The only changes is NgcWebpackPlugin -> AotPlugin rename and some cleanup in plugin arguments.

delagen avatar Oct 10 '16 04:10 delagen

With b17 I still don't get the lazy route modules compiled by ng build --prod --aot. There isn't even an ngfactory file, just a main bundle. For some reason the loadChildren does not trigger the compilation.

I have a file app/src/lazy/lazy.routes.ts:

export const lazyRoutes: Routes = [
    {
        path: 'signup',
        loadChildren: 'app/lazy/signup/signup.module#SignupModule'
    }
];

This route definition is part of the main route definition in app/app-routing.module.ts:

const routes: Routes = [
    {
        path: '',
        component: FrontpageComponent
    },
    {
        path: 'login',
        component: LoginComponent
    },
    ...lazyRoutes,
    {
        path: '**',
        component: NotFoundComponent
    },
];

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

I tried changing the loadChildren paths to be relative but then the JIT version throws as well, it has to start with app/.

achimha avatar Oct 10 '16 09:10 achimha

Just tested a project with beta.17. Build/serve without --aot work without issues w.r.t. lazy-loading, regardless of whether prod is used or not.

When I use ng serve --aot: Upon initial load the error message below appears in chrome debugger: error_1

While the initial screens seem to work regardless, hitting the lazy-loaded route throws an error, because the module ngFactory cannot be found.

One additional note, when I build with --aot the code chunk is not generated for the lazy route in the dist folder, however, there is a map file which seems to correspond to it.

tomwanzek avatar Oct 10 '16 23:10 tomwanzek

This error is also present in the demo app - webpack builds the project ok, but as soon as you click the lazy link, this error is thrown:

Uncaught (in promise): Error: Cannot find module './lazy.module.ngfactory'.

mattlewis92 avatar Oct 11 '16 14:10 mattlewis92

I get the error HomeModule is not an NgModule when using ng build --prod --aot on beta.17. Using just ng build --prod works fine.

home.module.ts

const HomeRoutes: Routes = [
  {
    path: '',
    component: HomeComponent,
    pathMatch: 'full'
  }
];

@NgModule({
  imports: [
    CommonModule,
    RouterModule.forChild(HomeRoutes)
  ],
  declarations: [HomeComponent]
})
export class HomeModule { }

app.module.ts

export const Routes = [
  { path: '', redirectTo: 'home', pathMatch: 'full' },
  { path: 'home', loadChildren: 'app/home/home.module#HomeModule' },
  { path: 'about', loadChildren: 'app/about/about.module#AboutModule' }
];

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    RouterModule.forRoot(Routes)
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

drejohnson avatar Oct 11 '16 16:10 drejohnson

Using lazy routes, v1.0.0-beta-16 wouldn't let me compile, but beta-17 completed successfully. I'm able to serve dev with great improvement in speed, but can't ng serve --prod --aot after ng build --prod --aot and still have the app fully load. Currently it only loads as far as the initial Loading... splash, and doesn't go any further, no errors in console.

Rosseyn avatar Oct 14 '16 23:10 Rosseyn

Any updates on this issue?

We have a large app that was originally written against beta-17. I recently moved it over to the Angular2 release and hooked up the ng compiler to take advantage of AoT compilation, but as of now I haven't found a way to leverage both AoT and lazy loading. As mentioned above, it will compile fine but when navigating to the lazy route it throws an error that 'lazy.module.ngfactory' could not be found

AoT is a killer feature and made a noticeable difference in our boot time, but at this point our --prod build still send down ~2.2mb all at once and we would love to cut that down by lazy loading some of our lesser-used routes

tony-fanbeat avatar Oct 20 '16 15:10 tony-fanbeat

Same thing here, as @drejohnson is experiencing...

ng build --aot fails with: "LazyModule is not an NgModule"

Created an app with ng new and then just added a lazy loaded module: https://github.com/MrCroft/lazy

ng build --aot fails with: "LazyModule is not an NgModule" if I don't use export default in the lazy module, or it fails with "can't find symbol LazyModule exported from module D:/wamp/www/lazy/src/app/lazy/lazy.module.ts" if I do have export default

angular-cli: 1.0.0-beta.18
Angular: 2.1.0
node: 6.9.1
os: win32 x64

NgxDev avatar Oct 21 '16 18:10 NgxDev

I just upgraded the project I tested with beta.17 to beta.18. As mentioned above, I am encountering the same failure modes:

  • project build and serve are fine without --aot (regardless of dev or prod)
  • project compiles without errors with --aot. However, the chunks for the lazy modules are not emitted in js. There is, oddly enough, again a partial 0.map without any corresponding js file.

tomwanzek avatar Oct 21 '16 20:10 tomwanzek

Any update on this?

naveedahmed1 avatar Oct 27 '16 00:10 naveedahmed1

Very strange... I've just tried ng build --aot with cli beta.18 on the last 3 releases of Angular: With Angular 2.1.0 I get: Cannot read property 'parse' of undefined With Angular 2.1.1 I get: this.compiler.compileModules is not a function With Angular 2.1.2 I get: Cannot read property 'transitiveModules' of undefined

This is unrelated to lazy loading (doesn't matter if I have a lazy module or not). Aot used to work just fine without lazy loading at least. Just ng new and then:

  1. change angular version in package.json (strict version, no ~ or ^)
  2. delete node_modules/@angular
  3. npm cache clean (maybe not necessary, but it's kind of a reflex already)
  4. npm install
  5. ng build --aot

In fact, even on the same example I gave in my post above (https://github.com/MrCroft/lazy) with the same Angular version (2.1.0) and same node/cli versions, I don't get LazyModule is not an NgModule anymore :| I now get Cannot read property 'parse' of undefined. Without changing anything, except uninstalling/installing different versions of Angular before going back to 2.1.0 again (but removed node_modules/@angular and running npm cache clean every time).

angular-cli: 1.0.0-beta.18
node: 6.9.1
os: win32 x64

Full logs: Angular 2.1.0:

D:\wamp\www\my-app> ng build --aot
can't resolve module @angular/core/src/di/opaque_token from D:\wamp\www\my-app\src\index.ts
Cannot read property 'parse' of undefined
TypeError: Cannot read property 'parse' of undefined
    at ReflectorHost.normalizeAssetUrl (D:\Users\iminar\Dev\angular\modules\@angular\compiler-cli\src\reflector_host.ts:70:28)
    at ReflectorHost.findDeclaration (D:\Users\iminar\Dev\angular\modules\@angular\compiler-cli\src\reflector_host.ts:175:27)
    at StaticReflector.initializeConversionMap (D:\Users\iminar\Dev\angular\modules\@angular\compiler-cli\src\static_reflector.ts:184:34)
    at new StaticReflector (D:\Users\iminar\Dev\angular\modules\@angular\compiler-cli\src\static_reflector.ts:72:57)
    at AotPlugin._setupOptions (D:\wamp\www\my-app\node_modules\@ngtools\webpack\src\plugin.js:122:27)
    at new AotPlugin (D:\wamp\www\my-app\node_modules\@ngtools\webpack\src\plugin.js:34:14)
    at Object.exports.getWebpackAotConfigPartial (D:\wamp\www\my-app\node_modules\angular-cli\models\webpack-build-typescript.js:57:13)
    at new NgCliWebpackConfig (D:\wamp\www\my-app\node_modules\angular-cli\models\webpack-config.js:18:42)
    at Class.run (D:\wamp\www\my-app\node_modules\angular-cli\tasks\build-webpack.js:17:22)
    at Class.run (D:\wamp\www\my-app\node_modules\angular-cli\commands\build.js:50:26)
    at Class.<anonymous> (D:\wamp\www\my-app\node_modules\angular-cli\lib\models\command.js:152:17)
    at tryCatch (D:\wamp\www\my-app\node_modules\rsvp\dist\lib\rsvp\-internal.js:215:12)
    at invokeCallback (D:\wamp\www\my-app\node_modules\rsvp\dist\lib\rsvp\-internal.js:230:13)
    at D:\wamp\www\my-app\node_modules\rsvp\dist\lib\rsvp\then.js:29:16
    at flush (D:\wamp\www\my-app\node_modules\rsvp\dist\lib\rsvp\asap.js:85:5)
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)

Angular 2.1.1:

D:\wamp\www\my-app> ng build --aot
  0% compilingthis.compiler.compileModules is not a function
TypeError: this.compiler.compileModules is not a function
    at CodeGenerator.codegen (D:\Users\iminar\Dev\angular\modules\@angular\compiler-cli\src\codegen.ts:71:26)
    at AotPlugin._make (D:\wamp\www\my-app\node_modules\@ngtools\webpack\src\plugin.js:186:43)
    at Compiler.<anonymous> (D:\wamp\www\my-app\node_modules\@ngtools\webpack\src\plugin.js:151:75)
    at Compiler.applyPluginsParallel (D:\wamp\www\my-app\node_modules\tapable\lib\Tapable.js:156:14)
    at Compiler.<anonymous> (D:\wamp\www\my-app\node_modules\webpack\lib\Compiler.js:463:8)
    at Compiler.applyPluginsAsync (D:\wamp\www\my-app\node_modules\tapable\lib\Tapable.js:73:70)
    at Compiler.compile (D:\wamp\www\my-app\node_modules\webpack\lib\Compiler.js:456:7)
    at Compiler.<anonymous> (D:\wamp\www\my-app\node_modules\webpack\lib\Compiler.js:219:10)
    at Compiler.readRecords (D:\wamp\www\my-app\node_modules\webpack\lib\Compiler.js:367:10)
    at Compiler.<anonymous> (D:\wamp\www\my-app\node_modules\webpack\lib\Compiler.js:216:9)
    at next (D:\wamp\www\my-app\node_modules\tapable\lib\Tapable.js:81:11)
    at Compiler.<anonymous> (D:\wamp\www\my-app\node_modules\webpack\lib\CachePlugin.js:34:58)
    at Compiler.applyPluginsAsync (D:\wamp\www\my-app\node_modules\tapable\lib\Tapable.js:85:13)
    at Compiler.<anonymous> (D:\wamp\www\my-app\node_modules\webpack\lib\Compiler.js:213:8)
    at next (D:\wamp\www\my-app\node_modules\tapable\lib\Tapable.js:81:11)
    at Compiler.<anonymous> (D:\wamp\www\my-app\node_modules\webpack\lib\node\NodeEnvironmentPlugin.js:23:3)
    at Compiler.applyPluginsAsync (D:\wamp\www\my-app\node_modules\tapable\lib\Tapable.js:85:13)
    at Compiler.run (D:\wamp\www\my-app\node_modules\webpack\lib\Compiler.js:210:7)
    at D:\wamp\www\my-app\node_modules\angular-cli\tasks\build-webpack.js:26:29
    at Class.run (D:\wamp\www\my-app\node_modules\angular-cli\tasks\build-webpack.js:25:16)
    at Class.run (D:\wamp\www\my-app\node_modules\angular-cli\commands\build.js:50:26)
    at Class.<anonymous> (D:\wamp\www\my-app\node_modules\angular-cli\lib\models\command.js:152:17)
    at tryCatch (D:\wamp\www\my-app\node_modules\rsvp\dist\lib\rsvp\-internal.js:215:12)
    at invokeCallback (D:\wamp\www\my-app\node_modules\rsvp\dist\lib\rsvp\-internal.js:230:13)
    at D:\wamp\www\my-app\node_modules\rsvp\dist\lib\rsvp\then.js:29:16
    at flush (D:\wamp\www\my-app\node_modules\rsvp\dist\lib\rsvp\asap.js:85:5)
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)

Angular 2.1.2:

D:\wamp\www\my-app> ng build --aot
  0% compilingCannot read property 'transitiveModules' of undefined
TypeError: Cannot read property 'transitiveModules' of undefined
    at D:\wamp\www\my-app\node_modules\@angular\compiler\bundles\compiler.umd.js:13042:22
    at Array.forEach (native)
    at analyzeNgModules (D:\wamp\www\my-app\node_modules\@angular\compiler\bundles\compiler.umd.js:13041:24)
    at OfflineCompiler.compileModules (D:\wamp\www\my-app\node_modules\@angular\compiler\bundles\compiler.umd.js:13115:20)
    at CodeGenerator.codegen (D:\Users\iminar\Dev\angular\modules\@angular\compiler-cli\src\codegen.ts:71:26)
    at AotPlugin._make (D:\wamp\www\my-app\node_modules\@ngtools\webpack\src\plugin.js:186:43)
    at Compiler.<anonymous> (D:\wamp\www\my-app\node_modules\@ngtools\webpack\src\plugin.js:151:75)
    at Compiler.applyPluginsParallel (D:\wamp\www\my-app\node_modules\tapable\lib\Tapable.js:156:14)
    at Compiler.<anonymous> (D:\wamp\www\my-app\node_modules\webpack\lib\Compiler.js:463:8)
    at Compiler.applyPluginsAsync (D:\wamp\www\my-app\node_modules\tapable\lib\Tapable.js:73:70)
    at Compiler.compile (D:\wamp\www\my-app\node_modules\webpack\lib\Compiler.js:456:7)
    at Compiler.<anonymous> (D:\wamp\www\my-app\node_modules\webpack\lib\Compiler.js:219:10)
    at Compiler.readRecords (D:\wamp\www\my-app\node_modules\webpack\lib\Compiler.js:367:10)
    at Compiler.<anonymous> (D:\wamp\www\my-app\node_modules\webpack\lib\Compiler.js:216:9)
    at next (D:\wamp\www\my-app\node_modules\tapable\lib\Tapable.js:81:11)
    at Compiler.<anonymous> (D:\wamp\www\my-app\node_modules\webpack\lib\CachePlugin.js:34:58)
    at Compiler.applyPluginsAsync (D:\wamp\www\my-app\node_modules\tapable\lib\Tapable.js:85:13)
    at Compiler.<anonymous> (D:\wamp\www\my-app\node_modules\webpack\lib\Compiler.js:213:8)
    at next (D:\wamp\www\my-app\node_modules\tapable\lib\Tapable.js:81:11)
    at Compiler.<anonymous> (D:\wamp\www\my-app\node_modules\webpack\lib\node\NodeEnvironmentPlugin.js:23:3)
    at Compiler.applyPluginsAsync (D:\wamp\www\my-app\node_modules\tapable\lib\Tapable.js:85:13)
    at Compiler.run (D:\wamp\www\my-app\node_modules\webpack\lib\Compiler.js:210:7)
    at D:\wamp\www\my-app\node_modules\angular-cli\tasks\build-webpack.js:26:29
    at Class.run (D:\wamp\www\my-app\node_modules\angular-cli\tasks\build-webpack.js:25:16)
    at Class.run (D:\wamp\www\my-app\node_modules\angular-cli\commands\build.js:50:26)
    at Class.<anonymous> (D:\wamp\www\my-app\node_modules\angular-cli\lib\models\command.js:152:17)
    at tryCatch (D:\wamp\www\my-app\node_modules\rsvp\dist\lib\rsvp\-internal.js:215:12)
    at invokeCallback (D:\wamp\www\my-app\node_modules\rsvp\dist\lib\rsvp\-internal.js:230:13)
    at D:\wamp\www\my-app\node_modules\rsvp\dist\lib\rsvp\then.js:29:16
    at flush (D:\wamp\www\my-app\node_modules\rsvp\dist\lib\rsvp\asap.js:85:5)
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)

Not sure what's with that D:\Users\iminar path in each of the above logs :)

NgxDev avatar Oct 28 '16 10:10 NgxDev

@MrCroft transitiveModules is the options of codegen function, if options does not provided then it undefined and cause this error source code from compiler-cli part https://github.com/angular/angular/blob/master/modules/%40angular/compiler-cli/src/main.ts#L23

delagen avatar Oct 28 '16 12:10 delagen

@Delagen Nobody said anything about doing anything else other than pass the --aot flag. Shouldn't ng new + ng build --aot simply just work?... Since I didn't do anything custom myself and there's no 3rd party involved or anything else... Just a minimum app, the one generated by the cli.

NgxDev avatar Oct 28 '16 13:10 NgxDev

I have created a new project using angular-cli beta.19-3 and just added a lazy route to reproduce the problem: https://github.com/sgybas/angular-cli-2452

ng serve works fine, ng serve --aot does not. The browser console shows Error: Cannot find module './lazy/lazy.module.ngfactory'.

ng build --prod works fine, two bundles are created. ng build --prod --aot creates the main bundle but the second chunk is not created.

Is there anything I can do to help resolve the issue?

sgybas avatar Nov 01 '16 10:11 sgybas

Could the maintainers provide us with some feedback regarding the AOT/lazy issues please? It'd be very helpful to know what we should expect.

achimha avatar Nov 01 '16 10:11 achimha