10

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.

Chellappan வ
  • 23,645
  • 3
  • 29
  • 60
user824624
  • 7,077
  • 27
  • 106
  • 183

4 Answers4

17

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 :)

slipper13
  • 45
  • 6
Pavel B.
  • 843
  • 1
  • 7
  • 17
10

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

Arthur Tsidkilov
  • 5,401
  • 2
  • 21
  • 18
  • 2
    Changing `"include": ["src/**/*.d.ts"]` to `"include": ["src/**/*.ts"]` worked for me -_- I would never have found this by myself – Jeremy Thille Apr 17 '20 at 11:41
  • This has worked for me too .... But I am getting now some other warning such as "...user-news.component.spec.ts is part of the TypeScript compilation but it's unused. Add only entry points to the 'files' or 'include' properties in your tsconfig." . I'm not sure whether I should worry about them or not at this point. – sandy Apr 27 '20 at 20:29
  • or add "src/**/*.module.ts" to not load as much – CRice Jul 13 '20 at 01:23
3

In my particular case, Pavel B. solution worked best. No warnings at compilation or runtime!

  {
    path: 'users',
    loadChildren: () => import('./users/users.module').then(mod => mod.UsersModule)
  }

Using:

Angular CLI: 9.1.7 Node: 12.14.0 OS: darwin x64

Angular: 9.1.9 ... animations, common, compiler, compiler-cli, core, forms ... platform-browser, platform-browser-dynamic, router Ivy Workspace: Yes

Package and Versions

@angular-devkit/architect         0.901.7
@angular-devkit/build-angular     0.901.7
@angular-devkit/build-optimizer   0.901.7
@angular-devkit/build-webpack     0.901.7
@angular-devkit/core              9.1.7
@angular-devkit/schematics        9.1.7
@angular/cli                      9.1.7
@ngtools/webpack                  9.1.7
@schematics/angular               9.1.7
@schematics/update                0.901.7
rxjs                              6.5.5
typescript                        3.8.3
webpack                           4.42.0
Frank_MCZ
  • 31
  • 3
0

You can use like this:

const rootRoutes: Routes = [ { path: 'site', loadChildren: () => SiteModule } ];