6

My folder structure is:

+-- app.module.ts
+-- app.routing.ts
+-- app.component.ts
+-- account/
|   +-- account.module.ts
|   +-- account.routing.ts
|   +-- account.component.ts

In my app.routing.ts I have,

  { path: 'account', loadChildren: './account/account.module#AccountModule' },

And, in account.routing.ts, I have,

  { path: 'login', component: LoginFormComponent}

but when I enter page/account/login I get the following error:

NodeInvocationException: Uncaught (in promise): Error: Cannot find module './account/account.module'. Error: Cannot find module './account/account.module'.

I've tried changing ./account/account.module#AccountModule to app/account/account.module#AccountModule, same error.

Any thoughts? Thanks in advance.

4
  • Have you tried /app/account/account.module#AccountModule ? Relative paths in Angular causes strange unexpected results all the freaking time. Commented Oct 20, 2017 at 17:50
  • @I.R.R. no success :( Commented Oct 20, 2017 at 17:53
  • How about other parts of your code? Where is the promise getting it's data? I once had to spend two days on a very very similar problem because I did not put two dots (..) and a slash (/) in front of my API endpoint, just to give you an idea on the kind of debugging you can expect with this paths problem. Commented Oct 20, 2017 at 17:56
  • Can you show AccountModule code? Have to tried eager loading? if yes, did that work? Commented Oct 20, 2017 at 18:06

4 Answers 4

14

you can try changing

  { path: 'account', loadChildren: './account/account.module#AccountModule' }

to

import {AccountModule} from './path';

{ path: 'account', loadChildren: ()=> AccountModule' }

note - do not forget to import module as above

4
  • it should be the correct answer. My code not work with the first way: { path: 'account', loadChildren: './account/account.module#AccountModule' }
    – HungNM2
    Commented Jul 10, 2018 at 23:50
  • I don't know why the original method of implementing loadChildren isn't working, but this did solve the issue for me.
    – Shawn
    Commented Aug 12, 2019 at 12:40
  • I expect symbolic link/junction or parent path with spaces might be an issue. Original works for colleague but not for me (And I use ntfs junction with path containing spaces).
    – Piro
    Commented Sep 19, 2019 at 12:55
  • That solved my problem. But I don't understand: how the hell did you get that idea? I would never have thought about that. Thanks! Commented Feb 13, 2021 at 2:25
3

The string version of loadChildren has been deprecated in angular 8

See: https://github.com/angular/angular/blob/master/CHANGELOG.md#800-2019-05-28 See: https://angular.io/api/router/LoadChildren

New syntax in angular 8/9

The new syntax takes a dynamic import expression. This is a function that returns an import. It is critical that you import your module there. Otherwise you are not lazy loading the module, but eagerly loading it.

{ 
path: 'account', 
loadChildren: ()=> import('./account/account.module').next(mod => mod.AccountModule) 
}

The docs: https://angular.io/api/router/LoadChildrenCallback

New syntax in angular 10

In angular 10 the syntax slightly changed again. Now it provides the import as a result of a JavaScript Promise ( import returns a Promise: ):

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

Warning: Assigning a module after importing will not lazy load your module!

S.Pradeep's solution will still eagerly load the module! You can check for yourself by implementing both approaches while checking the new (lazy loaded) network request navigating to a lazy loaded path.

3
  • This solution work well in Angular10. Old way: { path: 'account', loadChildren: './account/account.module#AccountModule' } is work in Angular 6. Commented Aug 15, 2020 at 5:30
  • In Angular10, change a little bit as: { path: 'account', loadChildren: ()=> import('./account/account.module').then(mod => mod.AccountModule) } Commented Aug 15, 2020 at 5:39
  • @ChinaHelloWorld Thx for the comment! I updated the answer to also provide angular 10's syntax ;). Commented Aug 19, 2020 at 9:29
0

Make sure that your account module sould have a single route like below

code for routing which will be routed once the module is lazy loaded

const routes: Routes = [{
  path: ':something',
  component: AccountComponent,
}];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class AccountRoutingModule {
}

Calling the routing module in core module

@NgModule({
  imports: [
    CommonModule,
    NgbModule.forRoot(),
    AccountRoutingModule,
    ...
1
  • 1
    Looking at his setup, looks like he's trying to do lazy loading. This solution will make it eager load. Commented May 27, 2018 at 17:31
0

I had the same issue, i tried couple of things. But changing app.routing.ts does the trick.

just replace your line with :

{ path: 'account', loadChildren: () => AccountModule },

I hope it helps

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.