3
votes

In my angular 6 application I'm trying to dynamically load a module using SystemJsNgModuleLoader.load() but it always fails with a "Cannot find module" error.

This small sample program on GitHub reproduces the problem. It is nothing more than a new Angular-CLI generated project with one additional module which I try to load dynamically. It fails with the following error message in the console:

Error: Uncaught (in promise): Error: Cannot find module "src/app/lazy/lazy.module".

What's going wrong here?


Note: I was trying to reproduce the problem on StackBlitz before. However that was misleading since the error message was slightly different:

Error: Uncaught (in promise): Error: Cannot find 'LazyModule' in 'app/lazy/lazy.module'

It turned out (thanks to Cam Plimsoll) that I was just using the wrong path in that example.

1
Those look similar same but I believe they are very different errors. - Aluan Haddad
Maybe you're right. But still I hope knowing what's wrong with the toy example will help me understand why my real application is not working. - Robert Hegner

1 Answers

0
votes

Load the module with the path starting from the sourceRoot

this.loader.load('src/app/lazy/lazy.module#LazyModule').then(module=>{     
      //handle the logic of inserting your component
    });