Solvedangular Angular upgrade to version 7 lazy loaded module cannot be found

Bug, feature request, or proposal:
After upgrading to angular 7, I received an error saying that the after login module is not being found to be loaded lazily.

What is the expected behavior?

To upgrade to angular 7 without changing anything in codes and scripts regarding module and routing files.

What is the current behavior?

Error at the console after clicking on Login, that the module is not found, despite that it was working in the previous version (Angular 6).

What are the steps to reproduce?

https://stackblitz.com/edit/angular-7-lazy-loading-soshba

Is there anything else we should know?

UPDATE On my question

I have added "BaseUrl": "./", to tsconfig.app.json and I got the following error:

error TS5023: Unknown compiler option 'BaseUrl'.

I think they mean in tsconfig.json and the baseUrl already added in.

END of Update

I updated my angular project to angular 7 using the following commands:

npm install @angular/animations@latest @angular/common@latest
    @angular/compiler@latest @angular/core@latest @angular/forms@latest 
    @angular/http@latest @angular/platform-browser@latest 
    @angular/platform-browser-dynamic@latest 
    @angular/platform-server@latest 
    @angular/router@latest --save

And:

npm install @angular-devkit/build-angular@latest 
@angular/compiler-cli@latest 
@angular/cli@latest 
@angular/language-service@latest --save-dev

And:

npm install core-js@latest zone.js@latest --save

And:

npm install @types/jasmine@latest @types/node@latest 
codelyzer@latest karma@latest karma-chrome-launcher@latest 
karma-cli@latest karma-jasmine@latest 
karma-jasmine-html-reporter@latest 
jasmine-core@latest jasmine-spec-reporter@latest 
protractor@latest tslint@latest --save-dev

And:

npm install typescript@latest --save-dev
npm install rxjs@latest rxjs-compat@latest --save

npm install rxjs-tslint@latest --save-dev

The resulting package.json is:

{
  "name": "angular",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@agm/core": "^1.0.0-beta.3",
    "@angular/animations": "^7.0.3",
    "@angular/cdk": "^7.0.3",
    "@angular/common": "^7.0.3",
    "@angular/compiler": "^7.0.3",
    "@angular/core": "^7.0.3",
    "@angular/flex-layout": "^7.0.0-beta.19",
    "@angular/forms": "^7.0.3",
    "@angular/http": "^7.0.3",
    "@angular/material": "^7.0.3",
    "@angular/material-moment-adapter": "^7.0.3",
    "@angular/platform-browser": "^7.0.3",
    "@angular/platform-browser-dynamic": "^7.0.3",
    "@angular/platform-server": "^7.0.3",
    "@angular/router": "^7.0.3",
    "angularx-qrcode": "^1.2.4",
    "bootstrap": "^4.1.3",
    "core-js": "^2.5.7",
    "font-awesome": "^4.7.0",
    "hammerjs": "^2.0.8",
    "html2canvas": "^1.0.0-alpha.12",
    "jspdf": "^1.4.1",
    "moment": "^2.22.2",
    "rxjs": "^6.3.3",
    "rxjs-compat": "^6.3.3",
    "tether": "^1.4.4",
    "zone.js": "^0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^0.10.5",
    "@angular/cli": "^7.0.5",
    "@angular/compiler-cli": "^7.0.3",
    "@angular/language-service": "^7.0.3",
    "@types/jasmine": "^2.8.11",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "^10.12.3",
    "codelyzer": "^4.5.0",
    "jasmine-core": "^3.3.0",
    "jasmine-spec-reporter": "^4.2.1",
    "karma": "^3.1.1",
    "karma-chrome-launcher": "^2.2.0",
    "karma-cli": "^1.0.1",
    "karma-coverage-istanbul-reporter": "~2.0.0",
    "karma-jasmine": "^1.1.2",
    "karma-jasmine-html-reporter": "^1.4.0",
    "protractor": "^5.4.1",
    "rxjs-tslint": "^0.1.5",
    "ts-node": "~5.0.1",
    "tslint": "^5.11.0",
    "typescript": "^3.1.6",
    "webpack": "^4.25.1"
  }
}

On ng serve --port=8000 I am getting the following error:

core.js:12584 ERROR Error: Uncaught (in promise): Error: Cannot find
module '../app/dashboard/dashboard.module' Error: Cannot find module
'../app/dashboard/dashboard.module'
at $_lazy_route_resource lazy namespace object:5
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke
(zone.js:388)
at Object.onInvoke (core.js:14143)
at

It was working when I was using Angular 6, and i received it only after upgrading.

I tried the solution mentioned on github:

Efectively, there are three posible solutions:

a) Using relative path loadChildren: './...

b) Using absolute path starting with loadChildren: 'src/app/...

c) tsconfig.app.json --> BaseUrl: "./"

And neither of them worked for me.

Here is my startup routing file:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { LoginComponent } from './login/login.component';
import { ForgotPasswordComponent } from './forgot-password/forgot-password.component';

const routes: Routes = [
  {
    path: 'login',
    component: LoginComponent
  },
  {
    path:'',
    component: LoginComponent
  },
  {
    path: 'forgot',
    component: ForgotPasswordComponent
    //canActivate: [AuthGuardService]
  },
  {
    path:'dashboard',
    loadChildren: './app/dashboard/dashboard.module#DashboardModule'
    
  },
  {
    path: '**',
    redirectTo: 'login',
    pathMatch: 'full'
  }
];

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

Am I missing an upgrade or Angular 7 implies us to change routing urls for a specific new form?

Quoting another solution from the github answer which assume that the problem is from the form of baseUrl:

@filipesilva I saw your commit angular/devkit@33af719 The property has
moved from tsconfig.app.json to src/tsconfig.json file, but not it's
value ./, therefore newly generated v6 apps by CLI wont be able to
find lazy load modules with paths defined as lazy load documentation.

I suggest this change. It preserves baseUrl: "./" on tsconfig.json and
each project have its tsconfig.app.json that overwrites baseUrl
property. I hope this is aligned with the multiprojects approach and
tooling.

If you prefere to use relative url for loadChildren, It should be
updated on documentation and tutorial.

22 Answers

✔️Accepted Answer

I had a similar issue, for me what worked as turning off enableIvy. I set up the router as specified in this

in tsconfig.json I enabled ivy via

  "angularCompilerOptions": {
    "enableIvy": true
  }

and I believe that for the Ivy compiler, the way you set up lazy loaded modules in your router modules is different. After commenting out this out, the route with the lazy loaded module worked as expected.

Other Answers:

I had the same issue. Try to load child module like this, this worked for me.
{ path : 'account/information', loadChildren: () => AccountInformationModule }

UPD: Sorry, this is not actually lazy loading. Need to find another solution.

I had the same issue. Try to load child module like this, this worked for me.
{ path : 'account/information', loadChildren: () => AccountInformationModule }

Not working in AOT.
I am getting the following error:
main.6bbb8e3847272062f17e.js:1 ERROR Error: Uncaught (in promise): Error: Runtime compiler is not loaded Error: Runtime compiler is not loaded at Ai (main.6bbb8e3847272062f17e.js:1) at t.compileModuleAsync (main.6bbb8e3847272062f17e.js:1) at e.project (main.6bbb8e3847272062f17e.js:1) at e._tryNext (main.6bbb8e3847272062f17e.js:1) at e._next (main.6bbb8e3847272062f17e.js:1) at e.next (main.6bbb8e3847272062f17e.js:1) at t._subscribe (main.6bbb8e3847272062f17e.js:1) at t._trySubscribe (main.6bbb8e3847272062f17e.js:1) at t.subscribe (main.6bbb8e3847272062f17e.js:1) at t.call (main.6bbb8e3847272062f17e.js:1) at Ai (main.6bbb8e3847272062f17e.js:1) at t.compileModuleAsync (main.6bbb8e3847272062f17e.js:1) at e.project (main.6bbb8e3847272062f17e.js:1) at e._tryNext (main.6bbb8e3847272062f17e.js:1) at e._next (main.6bbb8e3847272062f17e.js:1) at e.next (main.6bbb8e3847272062f17e.js:1) at t._subscribe (main.6bbb8e3847272062f17e.js:1) at t._trySubscribe (main.6bbb8e3847272062f17e.js:1) at t.subscribe (main.6bbb8e3847272062f17e.js:1) at t.call (main.6bbb8e3847272062f17e.js:1) at j (polyfills.71df7d5eaf99b7072328.js:1) at j (polyfills.71df7d5eaf99b7072328.js:1) at polyfills.71df7d5eaf99b7072328.js:1 at e.invokeTask (polyfills.71df7d5eaf99b7072328.js:1) at Object.onInvokeTask (main.6bbb8e3847272062f17e.js:1) at e.invokeTask (polyfills.71df7d5eaf99b7072328.js:1) at t.runTask (polyfills.71df7d5eaf99b7072328.js:1) at d (polyfills.71df7d5eaf99b7072328.js:1)

This solution worked for me: angular/angular-cli#9680 (comment)

Don't know if this is the same problem but for anyone experiencing what we were experiencing over here:

We were building an array of children routes to put into multiple spots in our router. This way you could access a user's profile for example from multiple routes.

The act of building the array of multiple routes was ending up at the "Module not found" error SOMETIMES, but not all the time. (Thinking race condition)

We fixed this by making a constant of the shared children routes and combining them with the tab specific routes. Example:

const subRoutes = [
  {
    path: 'u/:user_id',
    loadChildren: '../user-detailed/user-detailed.module#UserDetailedPageModule'
  }
];

const routes: Routes = [
  {
    path: 'tabs',
    component: TabsPage,
    children: [
      {
        path: 'users',
        children: [
              { path: '', loadChildren: './tab-users/tab-users.module#TabUsersPageModule' }, 
              ...subRoutes
             ]
      }
    ]
  }
];

Related Issues:

608
angular Angular5.x lazyLoad problem, undefined is not a function
For others that find this issue via Google as i did: I had the same problem when trying to lazy load...
348
angular Cyclic dependency error with HttpInterceptor
I resolved simply not setting authService in constructor but getting in the intercept function. ...
277
angular Uncaught Error: Can't resolve all parameters for ...
You are missing an @Injectable() annotation on your ApiService Support requests like these should li...
266
angular Force reload/refresh current route with RouteReuseStrategy
Hi If you really need to trick the Router into reloading the component on each routerLink click ...
260
angular Misleading error message "Cannot find a differ supporting object '[object Object]'"
I just ran into the same issue I'm not sure if the recommended solution will work for my case ...
224
angular update 2 to 4 has problem [ts] Property 'map' does not exist on type 'Observable<Response>'.
I met the same problem with the angular cli 6.0.0 and rxjs 6.1.0 And I solved the problem by replaci...
170
angular Angular2 AoT Compiler Errors
pls try /cc @chuckjaz When I try to compile my project with ngc it throws the below error: Error: Er...
152
angular HttpClient.delete() cannot handle a body in its request
It would be great to have body param in .delete() We just migrated our project to HttpClient and for...
140
angular Http - Observable completed function not firing
Third callback haven't been called when error occures ES6 promises hasn't method finally only then a...
133
angular Using multiple components in different modules causing "Type X is part of the declarations of 2 modules" error
as @brandonroberts saids create a shared module like this: then use the SharedModule like this.. ...
112
angular Unsupported platform for fsevents@1.0.14: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
@DzmitryShylovich did you edit package.json only? if npm-shrinkwrap.json is still there please remov...
98
angular Angular v5 ngc compiler: Error encountered in metadata generated for exported symbol 'Subscription'
Got this problem I'm submitting a.. Current behavior Building an Angulary library using 5.0.0-beta.4...
98
angular 404 on route refresh in angular 4
Got it working Just adding .htaccess in root I'm submitting a.. Current behavior I created a new com...
97
angular routerLinkActive not updating when routerLink changed
I have a hack that seems to work After looking at the source code it looks like this.update() is als...
97
angular No provider for HttpClient!
If you are using angular v5 version import HttpClientModule in your app.module.ts after HttpModule T...
81
angular ɵDomAnimationEngine and ɵNoopAnimationEngine module missing in animations 4.2.1
@dubedoy I installed @angular/animations@4.1.3 and it worked again. I'm submitting a .. ...
80
angular Error: Runtime compiler is not loaded in angular6 --prod mode
Do not and i repeat do not import your feature modules in app module and also when addressing featur...
79
angular appending header in HttpHeaders from '@angular/common/http' doesn't work
@trotyl I didn't understand your comment I'm submitting a.. ...
77
angular Can't bind to 'formGroup' since it isn't a known property of 'form'
did you import ReactiveFormsModule? I'm submitting a .. ...
73
angular AOT Compiler requires public properties, while non-AOT allows private properties
@aluanhaddad you have a big misunderstanding in here There is no subset of Typescript in here No one...
73
angular [Bug] angular/elements: Failed to construct 'HTMLElement': Please use the 'new' operator
Hi I have solved this issue by changing the target:es5 in the tsconfig.json to target:es2015 these i...
70
angular Cannot run angular 2+ from file:/// - looks like 'base href="/"' is the issue
Thanks @Markovy @audrummer15 I got it working completely in a fairly complex angular 2 app with mult...
69
angular HttpClient fails to parse an empty 200 response in IE11
For my error I was able to fix the problem by setting the responseType: to 'text' in the options ...
66
angular Function calls are not supported in decorators when fullTemplateTypeCheck is not specified and @dynamic has no effect
Regarding Ward's repro: @wardbell The build will succeed / fail depending on the combination of angu...
62
angular error TS2451: Cannot redeclare block-scoped variable 'ngDevMode'
had to add this line in the main tsconfig I'm submitting a.. ...
62
angular Problem with ngFor
Wouldn't [(ngModel)]=testItems[i] do the trick? I think that the error is saying that you can assign...
61
angular Issue with importing Observable from rxjs/Rx (import-blacklisted)
You shouldn't import from 'rxjs' or 'rxjs/Rx' since either import will import the whole of rxjs whic...
53
angular [RC5]: Minified bundle breaks
@robertoforlani Hopefully someone will have time to write a comprehensive explanation soon In the me...
53
angular router-outlet is appending rather than replacing when using BrowserAnimationsModule
Trying this solved the problem for me: this.zone.run(() => { this.router.navigate(['/main']); }); Re...
52
angular Lazy loaded module in named outlet throws error
We have this Where proxy route component is simply [x] bug report [ ] feature request [ ] support re...
52
angular IVY Error NG6002: Appears in the NgModule.imports of AppModule, but could not be resolved to an NgModule class
Not sure this will provide anyone relief or assist with figuring out what the root cause is but clea...
51
angular How to run angular 2 application on apache hosting server
Sorry to rock the boat I hope this doesn't attract more questions I'm only going to comment once :) ...
51
angular Support adding rel=canonical link tags using an included service
Eventually there will be some DocumentService part of Core that will handle both Meta/Link elements ...
50
angular Concept of Angular (ngZone + ChangeDetection) better than concept React, Vue (Virtual DOM)?
Concept of Angular (ngZone + ChangeDetection) better than concept React Vue (Virtual DOM)? If you ca...
50
angular I'd like to be able to use ngModel without specifying a name
Thank you all for the great feedback - very helpful! Here's how we are thinking about it: In the cas...
49
angular Model values not trimming automatically in angular 2
@laskoviymishka White space it already something If you are a programmer and think globally - yes ...
46
angular HttpClient - HttpErrorResponse not json but blob
I created this interceptor as a temporary solution until this one is fixed: I'm submitting a.. ...
45
angular Router's ActivatedRoute data returns empty {} if module is lazy
data is available only with this hell-like construction: And this is if you have children: ...
44
angular Misleading errormessage when using HostBinding with @animation trigger but no defined animations
The error message is not fine The error message says you're importing BrowserAnimationsModule incorr...
42
angular Memory leak when FormControlName created/destroyed few times
This issue has been around for nearly 3 years now (I usually don't like to start a message this way ...
41
angular Remove System.import() usage in favor of import()
I use a parser rule in my webpack configuration to disable the warning: https://webpack.js.org/confi...
41
angular Async event subscriber not updating UI after async call
Hi! The issue is that the async call result is outside ngZone thus not triggering the UI update You ...
40
angular Using router.navigate to navigate to another component does not invoke the onInit method
I have the same issue Angular is running in a Cordova app for iOS I tried the router-version 4.1.3 (...
37
angular Router: AoT compilation fails when using a function with loadChildren
Calling functions or calling new is not supported in metadata when using AoT This includes things li...
35
angular Provide a mock service using TestBed
I was having this issue as well however I noticed that my @component metadata still had the provider...
35
angular Angular2 download excel file from Web API, file is corrupt
@healkar01 I had the same issue and I resolved using native angular2 http request in this way: Backe...
35
angular 4.0.0-rc.6 [platform-server] - Cannot find module '@angular/animations/browser'. & other errors
(Just incase others find it) Make sure @angular/animations is installed as a dependency and the erro...
35
angular HttpClient mapping to typescript types not working
I agree with all the previous comments I find the syntax misleading widget.service.zip widget.servic...