angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Lazy loading module from library package located in node_modules fails to build.

Open thekhoi opened this issue 9 years ago • 80 comments

Bug Report or Feature Request (mark with an x)

- [x] bug report -> please search issues before submitting
- [ ] feature request

Versions.

@angular/cli: 1.0.4
node: 6.10.2
os: win32 x64
@angular/cli: 1.0.4
@angular/common: 4.1.3
@angular/compiler: 4.1.3
@angular/core: 4.1.3
@angular/forms: 4.1.3
@angular/http: 4.1.3
@angular/platform-browser: 4.1.3
@angular/platform-browser-dynamic: 4.1.3
@angular/router: 4.1.3
@angular/compiler-cli: 4.1.3

Repro steps.

I've modified the quick start library from @filipesilva and copied the output folder to my node_modules/quickstart-lib directory.

https://github.com/filipesilva/angular-quickstart-lib

Modification includes a routing module to add a child route.

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { LibComponent } from './component/lib.component';
const childRoutes: Routes = [
  {
    path: '',
    component: LibComponent
  }
];

@NgModule({
  imports: [
    RouterModule.forChild(childRoutes)
  ],
  exports: [
    RouterModule
  ]
})
export class LibRoutingModule { }

which is then imported into the main LibModule file.

import { NgModule } from '@angular/core';
import { LibRoutingModule } from './lib-routing.module';
import { LibComponent } from './component/lib.component';
import { LibService } from './service/lib.service';

@NgModule({
  declarations: [LibComponent],
  imports: [LibRoutingModule],
  providers: [LibService],
  exports: [LibComponent]
})
export class LibModule { }

In my host application, I created a simple route to lazy load the module from the library

const routes: Routes = [
  {
    path: 'quickstart',
    loadChildren: 'quickstart-lib#LibModule'
  },
  {
    path: 'preferences',
    loadChildren: 'app/preferences#PreferencesModule'
  },
  ...HomeRoutes
];


The log given by the failure.

PS C:\Workspace\scratch\Ng4Starter\host> ng serve
** NG Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200 **
 11% building modules 14/23 modules 9 active ...gular\platform-browser-dynamic.es5.js(node:19124) DeprecationWarning: loaderUtils.parseQuery() received a non-string value which can be problematic,
 see https://github.com/webpack/loader-utils/issues/56
parseQuery() will be replaced with getOptions() in the next major version of loader-utils.
 11% building modules 15/23 modules 8 active ...gular\platform-browser-dynamic.es5.js(node:19124) DeprecationWarning: loaderUtils.parseQuery() received a non-string value which can be problematic,
 see https://github.com/webpack/loader-utils/issues/56
parseQuery() will be replaced with getOptions() in the next major version of loader-utils.
Hash: c46342c7aafb5ce0f53e
Time: 14214ms
chunk    {0} 0.chunk.js, 0.chunk.js.map 20.5 kB {3} [rendered]
chunk    {1} 1.chunk.js, 1.chunk.js.map 793 bytes {0} {3} [rendered]
chunk    {2} polyfills.bundle.js, polyfills.bundle.js.map (polyfills) 157 kB {7} [initial] [rendered]
chunk    {3} main.bundle.js, main.bundle.js.map (main) 72.5 kB {6} [initial] [rendered]
chunk    {4} styles.bundle.js, styles.bundle.js.map (styles) 229 kB {7} [initial] [rendered]
chunk    {5} scripts.bundle.js, scripts.bundle.js.map (scripts) 162 kB {7} [initial] [rendered]
chunk    {6} vendor.bundle.js, vendor.bundle.js.map (vendor) 3.36 MB [initial] [rendered]
chunk    {7} inline.bundle.js, inline.bundle.js.map (inline) 0 bytes [entry] [rendered]

ERROR in ./~/quickstart-lib/quickstart-lib.d.ts
Module build failed: Error: Debug Failure. False expression: Output generation failed:=>C:/Workspace/scratch/Ng4Starter/host/node_modules/quickstart-lib/quickstart-lib.d.ts
    at Object.assert (C:\Workspace\scratch\Ng4Starter\host\node_modules\typescript\lib\typescript.js:3259:23)
    at Object.transpileModule (C:\Workspace\scratch\Ng4Starter\host\node_modules\typescript\lib\typescript.js:76181:18)
    at TypeScriptFileRefactor.transpile (C:\Workspace\scratch\Ng4Starter\host\node_modules\@ngtools\webpack\src\refactor.js:161:27)
    at Promise.resolve.then.then.then.then (C:\Workspace\scratch\Ng4Starter\host\node_modules\@ngtools\webpack\src\loader.js:361:37)
    at process._tickCallback (internal/process/next_tick.js:109:7)
 @ ./src async
 @ ./~/@angular/core/@angular/core.es5.js
 @ ./src/main.ts
 @ multi webpack-dev-server/client?http://localhost:4200 ./src/main.ts
webpack: Failed to compile.

Desired functionality.

I would like to distribute my code as an NPM library and have the CLI support lazy loading the module that is located in the 'node_modules' directory.

Modules located in the same application directory are lazy loaded just fine.

Mention any other details that might be useful.

https://github.com/filipesilva/angular-quickstart-lib

thekhoi avatar May 18 '17 18:05 thekhoi

Facing the same issue routing to external UMD module which is inside 'node_modules'

It throws below error while building application in --prod mode

Error typeerror

Please help us to solve the issue Thanks

smkart avatar Mar 19 '18 12:03 smkart

The interesting thing here is that I have different error:

With routing like:

const routes: Routes = [
  {
    path: 'libContent',
    loadChildren: 'my-lib#MyLibModule'
  }
];
...
  imports: [
    RouterModule.forRoot(routes),
...

it throws error like this:

my-lib\my-lib.d.ts is missing from the TypeScript compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property.

but it works if I pre-build it in the main bundle like so:

import { MyLibModule } from 'my-lib';

const routes: Routes = [
  {
    path: 'libContent',
    loadChildren: () => MyLibModule 
  }
];
...

It works just like expected

Nodarii avatar May 04 '18 14:05 Nodarii

@Nodarii It is @angular/compiler-cli that delegates to Typescript to resolve the my-lib module, and this assumes that there is a my-lib.ts file at node_modules/my-lib/my-lib.ts for us using ng-packagr, but instead there is only the typings. That's the error you are seeing, it is a red herring. It seems that @angular/compiler-cli should support resolving modules that support the Angular Package Format, and it currently does not.

vanslly avatar May 10 '18 20:05 vanslly

@Nodarii @vanslly I have same issue with angular cli v6.0.3. Did you guys solved that? or created issue?

batdelger avatar May 23 '18 03:05 batdelger

@batdelger, for now I do not use lazy loading, just load moule like so loadChildren: () => MyLibModule and will replace it when this issue will be fixed

Nodarii avatar May 23 '18 10:05 Nodarii

I got lazy loading working with cli, standard angular router (not ui), lazy loading and... a couple of workarounds.

Please find a working example here: https://github.com/danielecanteri/angular-lazy-loading-modules.

Basically I had to create a wrapper module (as suggested in comments above). In the wrapper module I had to re-register the child routes (exported from the module).

import {ExternalModuleLibModule, routes} from 'external-module-lib';
...
@NgModule({
  imports: [
    CommonModule,
    ExternalModuleLibModule,
    // hack: re-register routes for imported module
    RouterModule.forChild(routes)
  ],
  declarations: []
})
export class ExternalModuleWrapperModule { }

After that I can use lazy loading routes int the application as follows:

{
  path: 'external-module',
  loadChildren: './external-module-wrapper/external-module-wrapper.module#ExternalModuleWrapperModule'
},

danielecanteri avatar May 25 '18 13:05 danielecanteri

@danielecanteri I have tried your suggestion and I see it is working fine with 1 external lib module. I have multiple external lib modules and when I try what you have suggested I always get routed to the first imported module from the imports section.

Do you know if I am supposed to have a lazy wrapper for each module? From the other comments I have understand that only 1 wrapper is needed per external library but I have not managed to figure that part out.

import { ExternalModule1, ExternalModule2 } from "@namespace/external-lib";

@NgModule({
    imports: [
        ExternalModule1,
        ExternalModule2
    ]
  })
  export class LazyWrapperModule {}

emarzate avatar May 30 '18 14:05 emarzate

I see this as a seriously problematic issue for enterprise projects. Currently one of few issues keeping me away from using angular-cli.

Is there any chance at all for this issue to get resolved?

klemenoslaj avatar Jun 10 '18 21:06 klemenoslaj

I just decided to publish my lazy modules without bundling ( to our local npm registry). Actually it is not very bad thing. Building them at once is more safe. To do that you must add your lazy libraries to tsconfig.app.json

On Да, 2018 6-р сар 11 05:08 Klemen Oslaj [email protected] wrote:

I see this as a seriously problematic issue for enterprise projects. Currently one of few issues keeping me away from using angular-cli.

Is there any chance at all this issue will get resolved?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/angular/angular-cli/issues/6373#issuecomment-396082110, or mute the thread https://github.com/notifications/unsubscribe-auth/AC70Cu6qeRA8EyG0Xv8fjcTg3cBMcafVks5t7YrigaJpZM4Nfpev .

batdelger avatar Jun 11 '18 15:06 batdelger

I have created a Customer App as per https://angular.io/guide/lazy-loading-ngmodules; I also integrated a lib Angular 6 workspace lib module named inventory-lib; in my app-routing.module.ts file I implemented routing

const routes: Routes = [ { path: 'customers', loadChildren: 'app/customers/customers.module#CustomersModule' }, { path: 'orders', loadChildren: 'inventory-lib/orders.module#OrdersModule' }, { path: '', redirectTo: '', pathMatch: 'full' } ];

##Note: inventory-lib is not a actual folder but it is a ng6 workspace lib https://blog.angularindepth.com/creating-a-library-in-angular-6-87799552e7e5

Not the order route is not loading or no error in console

GITHUB repo to replicate error https://github.com/akhilshastri/lazy-loading-with-ng6-lib-module

akhilshastri avatar Jun 11 '18 18:06 akhilshastri

@batdelger yes I am doing the same now... Publishing modules without bundling, or using Angular CLI 6 libraries, but again I do not bundle them. This of course works, but it is still just a workaround.

klemenoslaj avatar Jun 11 '18 19:06 klemenoslaj

Seems to be the same problem I created an issue for https://github.com/angular/angular/issues/24519.

totkeks avatar Jun 19 '18 15:06 totkeks

Why hasn't this issue been a high priority for a fix? Are we not supposed to lazy load modules from Angular 6 libraries?

idwebmedia avatar Jul 22 '18 01:07 idwebmedia

Don't know why angular guys have become so irresponsive, the issue is causing us problems.

On Sun, Jul 22, 2018, 7:06 AM Anthony Prado [email protected] wrote:

Why hasn't this issue been a high priority for a fix? Are we not supposed to lazy load modules from Angular 6 libraries?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/angular/angular-cli/issues/6373#issuecomment-406834793, or mute the thread https://github.com/notifications/unsubscribe-auth/AcrHe3XaEUo-b0mkisndaGexq8uEJTIZks5uI9cYgaJpZM4Nfpev .

khanakifalam002 avatar Jul 22 '18 03:07 khanakifalam002

Any updates on the how we could directly load from node modules rather creating an wrapper

aburht avatar Jul 24 '18 18:07 aburht

For my current project it is necessary to reuse feature modules. So i´m looking forward for a solution because usinge the provided workaround is a huge overhead if you have multiple modules.

nekator avatar Jul 27 '18 08:07 nekator

Any update on this issue??

siddharth1903 avatar Aug 04 '18 14:08 siddharth1903

@filipesilva @Brocco any idea, if this issue is going to be considered or it would be great if you let us know if you would be fixing this issue anytime sooner

I understand there could be a reason, would you please give us Justification, why this is not considered, as this is open since a year back

Eagerly waiting for the response from angular cli team

aburht avatar Aug 07 '18 12:08 aburht

I've tried the @Nodarii 's solution, but didn't worked: loadChildren: () => MyLibModule But it worked using a named function: export function loadMyLibModule() { return MyLibModule; } ... loadChildren: loadMyLibModule

tiagoa avatar Sep 05 '18 12:09 tiagoa

I don't think this issue will be resolved in v6. We'll have to wait till at least 7 and possibly Ivy

idwebmedia avatar Sep 05 '18 14:09 idwebmedia