13

We can lazy load a local module like this module

    {
        path: 'somePpath',
        loadChildren: 'app/path/some.module#SomeModule'
    },

How do we lazy load a module that comes from an external library resides in node_modules?

5
  • what loader do you use? Commented May 17, 2017 at 12:37
  • Just tried it with angular-cli and loadChildren: '../../node_modules/lazy/lazy.module#LazyModule'. It works for me Commented May 17, 2017 at 12:39
  • @yurzui The context of "external library" almost certainly means exactly that and not an NgModule that we know works. So where the OP is asking to "lazy load a library" ( i.e lodash ), then that is just not what the feature here is about. Commented May 17, 2017 at 13:21
  • @NeilLunn OP wrote `How do we lazy load a module So i suggested he asked for NgModule Commented May 17, 2017 at 13:23
  • Maybe this issue will help you out: github.com/angular/angular-cli/issues/6373 Commented Sep 13, 2018 at 11:01

3 Answers 3

11

To load an external module in the router module you might need to use a Wrapper module. Create a wrapper module in the same local project in which you are having your routing module. Import your external module in this wrapper module with traditional import syntax.

import { SomeModule } from '@externalLib';

Include this module in the imports of NgModule's import array.

@NgModule({
  imports: [SomeModule]
})
export class SomeWrapperModule {
}

Then use this wrapper module in the router module as we usually use a module.

// for Angular 7 and below 
{
    path: 'some-path',
    loadChildren: '../somewrapper.module#SomeWrapperModule'
}

// for Angular 8+
{
    path: 'some-path',
    loadChildren: () => import('../somewrapper.module').then(mod => mod.SomeWrapperModule)
}
Sign up to request clarification or add additional context in comments.

1 Comment

I can confirm this approach of using a WrapperModule works - but with a caveat. If you are trying to import more than one module into the wrapper module, it will not work. You need to create a wrapper module for each external library you want to lazy load.
3

I'm not sure since which version, but at least since version 13 you can lazy load a node module anywhere directly in a method.

I use it, for example, when exporting data to excel, which is rarely run. Example:

public export(): void {
    import('xlsx').then(xlsx => {
        xlsx.writeFile(...);
    });
}

2 Comments

The problem is, when you try to load a feature module lazily i.e. SomeModule from 'some-lib' and use some components from that module like: <some-comp></some-comp>, you cannot just run import('some-lib').then() to use the components you want.
It's now possible in Angular 17 with Deferrable Views. See more info in angular.io/guide/defer, or netbasal.com/….
0

I have found other ways instead of creating a wrapper module which by the way is not very pleasant, follow below some more fancy approaches:

https://habr.com/en/post/442370/

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.