EXCEPTION: Uncaught (in promise): Error: Cannot find module 'app/home/home.module'

风流意气都作罢 提交于 2019-11-30 18:23:51

I landed on this question with very similar symptoms and context, so it seems useful to remark that this answer to another question helped me out.

In my specific case, I was somewhat following the lazy feature modules docs, and I even faithfully tried to replicate the associated StackBlitz example code. For some reason that example gets away with:

loadChildren: 'app/customers/customers.module#CustomersModule'

And even though my Angular CLI (v6) based experiment had similar folder structure, I needed to do either this:

// Full path including `src` at the start:
loadChildren: 'src/app/customers/customers.module#CustomersModule'

or this:

// Relative path from the `app-routing.module.ts` file:
loadChildren: './customers/customers.module#CustomersModule'

No clue why the StackBlitz example gets away with the first code example, but the other two both make sense and work for me when doing ng serve.

It seems that the angular-cli renderer has problems with lazy loading when you use export default class SomeModule { } ...along with a few other nuances.

This is what I did to resolve the same "Error: Cannot find module..." I was getting on Heroku deployment:

  • Source all loadChildren paths from the app root & include a hash for your module name
    • loadChildren: 'app/main/some-module/some-module.module#SomeModule'
  • Change export default class SomeModule { } to export class SomeModule { }

For me I had to use Module Map NgFactory Loader you can do:

npm install @nguniversal/module-map-ngfactory-loader --save

then add module-map-ngfactory-loader to your server module:

import {ModuleMapLoaderModule} from '@nguniversal/module-map-ngfactory-loader';

@NgModule({
  imports: [
    AppModule,
    ServerModule,
    ModuleMapLoaderModule
  ],
  bootstrap: [AppComponent],
})
export class AppServerModule {}
Zecide

I managed to make it work, here is what I done :

1 - Make the routing code in the module ( not a file )

2 - Make the module file in the parent directory of the component

3 - Delete the 'default' in the export like this

export DEFAULT class HomeModule { }

became

export class HomeModule { }

you can see that it works with beta 24 here : https://github.com/mauricedb/lazy-routes

I don't know what is happening !!!

Also take care of following in the file 'app-routing.module.ts' i.e

const routes: Routes = [ {path:'login',loadChildren:'./login/login.module#LoginModule'}, {path:'registration',loadChildren:'./registration/registration.module#RegistrationModule'} ];

in above the bold letters should be in capital

As silly as it sounds, on Angular 6.

I was using this command ng build --aot --watch while developing my application. Somehow getting in to the zone I saved a lot of files (copy paste from some other projects). The build did work but was not showing errors but browser showed this error.

I closed the build, & rebuilt it again & all the errors(unrelated to the above) which were not being shown showed up!!.

Vishal

Angular CLI: 6.1.5 Node: 8.11.3 OS: win32 x64 Angular: 6.1.6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!