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

Sign up to request clarification or add additional context in comments.

4 Comments

it should be the correct answer. My code not work with the first way: { path: 'account', loadChildren: './account/account.module#AccountModule' }
I don't know why the original method of implementing loadChildren isn't working, but this did solve the issue for me.
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).
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!
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 Comments

This solution work well in Angular10. Old way: { path: 'account', loadChildren: './account/account.module#AccountModule' } is work in Angular 6.
In Angular10, change a little bit as: { path: 'account', loadChildren: ()=> import('./account/account.module').then(mod => mod.AccountModule) }
@ChinaHelloWorld Thx for the comment! I updated the answer to also provide angular 10's syntax ;).
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 Comment

Looking at his setup, looks like he's trying to do lazy loading. This solution will make it eager load.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.