Universal: ERROR in ./src/app1/main.server.ts, Module not Found Error: Can't resolve 'app/.../...module.ngfactory.js' ( in latest CLI )

Created on 28 Nov 2017  路  10Comments  路  Source: angular/universal

Hello. I have a repo which you can clone and reproduce the issue (https://github.com/nekkon/angular-cli-library). After you install the node modules run npm run app1:ssr:build and you will see the errors. If you build the browser version of the app npm run app1:build everything gets built properly. Any suggestion/tip of what the problem could be? Any help would be greatly appreciated @gdi2290 + @Toxicable Thanks in advance for your great work so far.

Just to add more info about the errors: I think it has to do with lazy loading the modules in the app.routing.ts . They are lazy loaded properly when app is served or built, but they can not be found when they are built for server side rendering. Not sure what causes this. An example of the error shown ERROR in ./src/app1/main.server.ts, Module not Found Error: Can't resolve 'app/.../...module.ngfactory.js'

repro steps

Most helpful comment

@nekkon I just had a similar issue and in my case the solution was to make sure I imported ModuleMapLoaderModule as the last module in the imports array of my AppServerModule.

So this worked:

import {NgModule} from '@angular/core';
import {ServerModule} from '@angular/platform-server';
import {ModuleMapLoaderModule} from '@nguniversal/module-map-ngfactory-loader';

import {AppModule} from './app.module';
import {AppComponent} from './app.component';

@NgModule({
  imports: [
    AppModule,
    ServerModule,
    ModuleMapLoaderModule,
  ],
  bootstrap: [AppComponent],
})
export class AppServerModule {}

while this failed:

import {NgModule} from '@angular/core';
import {ServerModule} from '@angular/platform-server';
import {ModuleMapLoaderModule} from '@nguniversal/module-map-ngfactory-loader';

import {AppModule} from './app.module';
import {AppComponent} from './app.component';

@NgModule({
  imports: [
    ServerModule,
    ModuleMapLoaderModule,
    AppModule,
  ],
  bootstrap: [AppComponent]
})
export class AppServerModule {}

I'm not sure if this is the fix to your issue but it might :-)

All 10 comments

@nekkon I just had a similar issue and in my case the solution was to make sure I imported ModuleMapLoaderModule as the last module in the imports array of my AppServerModule.

So this worked:

import {NgModule} from '@angular/core';
import {ServerModule} from '@angular/platform-server';
import {ModuleMapLoaderModule} from '@nguniversal/module-map-ngfactory-loader';

import {AppModule} from './app.module';
import {AppComponent} from './app.component';

@NgModule({
  imports: [
    AppModule,
    ServerModule,
    ModuleMapLoaderModule,
  ],
  bootstrap: [AppComponent],
})
export class AppServerModule {}

while this failed:

import {NgModule} from '@angular/core';
import {ServerModule} from '@angular/platform-server';
import {ModuleMapLoaderModule} from '@nguniversal/module-map-ngfactory-loader';

import {AppModule} from './app.module';
import {AppComponent} from './app.component';

@NgModule({
  imports: [
    ServerModule,
    ModuleMapLoaderModule,
    AppModule,
  ],
  bootstrap: [AppComponent]
})
export class AppServerModule {}

I'm not sure if this is the fix to your issue but it might :-)

@beeman Thanks for the tip. I checked my server.module.ts and the modules were in the correct order. I believe it is something else.

Ye I tried ModuleMapLoaderModule as well but with no luck.

+1 on this issue. Non universal lazy load is working fine with enabled AOT. the universal compilation is failing and the ModuleMapLoaderModule sorting proposal is not working for me

@igmdvc I don't have any issues with using lazy modules.
The order of imports is important, double check that.
Also double check here https://github.com/angular/universal-starter to ensure you have it setup correctly.
If the issue still persists then please make a minimal reproduction.

Since no minimal reproduction has been produced im going to close this off.
If you still have an issue feel free to open another issue with a minimal reproduction

Having the exact same problem with all my lazy loaded modules...@nekkon did you solved it?

Any help to resolve this problem? I have added ModuleMapLoaderModule and in correct order as well. Still no luck.

For me, as beeman mentioned, adding _ModuleMapLoaderModule_ to _app.server.module.ts_ solved the issue. The order of the imported modules did matter also.

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

_This action has been performed automatically by a bot._

Was this page helpful?
0 / 5 - 0 ratings

Related issues

daright picture daright  路  6Comments

PatrickJS picture PatrickJS  路  5Comments

matthewharwood picture matthewharwood  路  4Comments

jeffwhelpley picture jeffwhelpley  路  4Comments

leon picture leon  路  4Comments