Nx: NX(v8): Lazy loading from LIB directory causes "module not found .. index.ngfactory" on prod build

Created on 7 Jun 2019  路  8Comments  路  Source: nrwl/nx

  • [x] I am running the latest version
  • [x] I checked the documentation and found no answer
  • [x] I checked to make sure that this issue has not already been filed
  • [x] I'm reporting the issue to the correct repository (not related to Angular, AngularCLI or any dependency)

Expected Behavior

To be able to load lazy modules in prod, it currently fails on a production build

Current Behavior

Using both ng server and ng build, all seems to go well. But doing an ng build --prod and any libraries that are in the libs folder causes the following error

Date: 2019-06-07T14:00:34.648Z
Hash: 7f5200abcc9a74395181
Time: 11800ms
chunk {0} runtime.5ba2ece7ab2ffcd4c683.js (runtime) 2.16 kB [entry] [rendered]
chunk {1} main.f737b50ade1e2927999b.js (main) 284 kB [initial] [rendered]
chunk {2} polyfills.079fa0a87867aebb25de.js (polyfills) 42.9 kB [initial] [rendered]
chunk {3} polyfills-es5.713635137766a4d7e797.js (polyfills-es5) 68 kB [initial] [rendered]
chunk {4} styles.09e2c710755c8867a460.css (styles) 0 bytes [initial] [rendered]
chunk {5} 5.3db1d83002b02c75fae1.js () 436 bytes  [rendered]

ERROR in ./src/app/app.module.ts
Module not found: Error: Can't resolve './../../../../libs/my-test/index.ngfactory' in '/Users/ian/Development/Personal/typescript/test/apps/test-ui/src/app'

I actually have 2 lazy loaded modules, the one inside app works, if i comment out the one inside lib then it builds, otherwise you get the error above.

The 2 modules are exactly the same and created for testing, they are using the new way of doing lazy loading of modules in angular 8.

RouterModule.forRoot(
    [{ path: '', loadChildren: () => import("./my-lazy-load/my-lazy-load-routing.module").then(m => m.MyLazyLoadRoutingModule)},
      { path: 'xyz', loadChildren: () => import("../../../../libs/my-test").then(m => m.MyLazyLoadModule)}
    ],
    { initialNavigation: 'enabled', enableTracing: true }
  )

Context

Please provide any relevant information about your setup:

  • version of Nx 8
  • version of Angular CLI 8
third-party bug

All 8 comments

Hi, thanks for reporting this issue.

  1. I strongly, strongly recommend you use @npmScope/my-test to lazy load. This is shorter and points to the correct file. It is also important for us to detect the lazy dependency to the lib.
  2. Does this file exist? ../../../../libs/my-test/index.ts. Usually, it would be ../../../../libs/my-test/src/index.ts.

Let me know if this helps fix the issue.

Hi,

Thanks for coming back to me. This was kind of a POC to post here :-) To demonstrate that its the same module etc loaded from different places and one works and the other doesn't

This is actually what I am doing

{ path: '', loadChildren: () => import("@app-ws/register").then(m => m.RegisterModule)}

and my Tsconifg

      "@app-ws/register": ["libs/register/src/index.ts"],

I confirm that above exist, also my IDE states it exists. For example chaning the m.RegisterModule and it will start to complain :-)

The index.ts is barrel

export {RegisterModule} from "./lib/register.module"
export {RegisterComponent} from "./lib/register.component"

Can i offer additional info ?

Also, I should highlight actually that using NG SERVE and NG BUILD (no prod) - works great - its only when passing the --prod

Hi @appsolutegeek. What happens if you change your barrel module export to export * from "./lib/register.module"?

I see. I get that error for the named export but not the * export. I believe this is an issue with the @angular/cli so let me make an issue there.

Please use the export * from './lib/lib.module.ts in the mean time.

@FrozenPandaz Nice catch! - It works!, I am using the workaround. Thank you.

Was this page helpful?
0 / 5 - 0 ratings