Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot find module in angular Lazy Loading

I have an angular project successfully on the Mac environment

Angular CLI: 7.0.5
Node: 8.11.3
OS: darwin x64
Angular: 7.0.3

Now I am running the same code on ubuntu 18.04 with the setup

Angular CLI: 7.3.9
Node: 12.9.1
OS: linux x64
Angular: 7.2.15

however it is coming with a very weird issue when trying to lazy load another module, I keep getting this error Error: Cannot find module "app/website/site.module"

Here is my project structure

enter image description here

and app-routing.module.ts

 const routes: Routes = [    
      {
        path: 'site',
        loadChildren: 'app/website/site.module#SiteModule',
      }
    ]

the routing is used to work in mac but failed in ubuntu

I looked up a different solution

const rootRoutes: Routes = [
  { path: 'site', loadChildren: './website/site.module#SiteModule' }
];

but still it failed to work.

like image 962
user824624 Avatar asked Sep 03 '19 03:09

user824624


People also ask

How to lazy load angular modules?

To lazy load Angular modules, use loadChildren (instead of component) in your AppRoutingModule routes configuration as follows. In the lazy-loaded module's routing module, add a route for the component.

How do I troubleshoot lazy-loading errors?

A common error when lazy-loading modules is importing common modules in multiple places within an application. Test for this condition by first generating the module using the Angular CLI and including the --route route-name parameter, where route-name is the name of your module.

How do I know if a module is lazy loaded?

You can check to see that a module is indeed being lazy loaded with the Chrome developer tools. In Chrome, open the developer tools by pressing Cmd+Option+i on a Mac or Ctrl+Shift+j on a PC and go to the Network Tab. Click on the Orders or Customers button.

Why is my--route parameter not working in angular?

If the Angular CLI generates an error when you use the --route parameter, but runs correctly without it, you might have imported the same module in multiple places. Remember, many common Angular modules should be imported at the base of your application. For more information on Angular Modules, see NgModules.


2 Answers

I created an example project on stackblitz

Explanation:

So first of all, you have to create your routes like this:

const routes: Routes = [
  // this will get lazy loaded
  { 
    path: 'lazyload',
    loadChildren: () => import('./module/module.module').then(mod => mod.ModuleModule) 
  },

  // default route
  { 
    path: '**',
    component: Component1Component
  }
];

Then add it to app module (root module):

@NgModule({
  imports:      [
    // your imports here ...
    // add your routes (forRoot() is only in the root module!)
    RouterModule.forRoot(routes)
  ],
  // your providers, declarations, etc.
})
export class AppModule { }

Then you have to add routes to the child module (in this case ModuleModule):

const routes: Routes = [
  { path: 'route1', component: Component2Component },
  { path: '', component: Component3Component }
];

@NgModule({
  imports: [
    // your imports here ... 
    RouterModule.forChild(routes)
  ],
  declarations: [Component2Component, Component3Component]
})
export class ModuleModule { }

now it should work, if you have any problems I will be here :)

like image 190
Pavel B. Avatar answered Sep 17 '22 16:09

Pavel B.


I had a similar problem, in my code it was the same path like your

  {
      path: 'admin', loadChildren: './admin/admin.module#AdminModule'
  }

and after I changed file tsconfig.app.json

it began to work and find child route, on attached image you can see changes that i done

enter image description here

like image 27
Arthur Tsidkilov Avatar answered Sep 18 '22 16:09

Arthur Tsidkilov