ng-packagr: AOT: Unexpected value 'undefined' exported by the module when using barrel index.ts

Type of Issue

[x] Bug Report
[ ] Feature Request

Description

I’m trying to migrate an existing library to the new Angular-cli library support This library has a lot of barrels index.ts files I’m experiencing error on build with AOT enabled

How To Reproduce

https://github.com/kepennar/ng-cli-lib-issue-repro

Given a library managed by the new angular-cli v6 library management system.

Given a component Comp1Component of this lib imported from a barrel index.ts in a module LibModule

projects/lib1/src/lib/components/index.ts

export * from './comp1.component';

projects/lib1/src/lib/lib.module.ts

import { Comp1Component } from './components';
// ...

@NgModule({
  declarations: [Comp1Component],
  exports: [Comp1Component]
})
export class LibModule {}

When an application using this lib is launched in prod mode

Then build crash with the error

ERROR in : Unexpected value 'undefined' exported by the module 'Lib1Module ...

If the component is directly imported from it’s own file. It’s work

projects/lib1/src/lib/lib.module.ts

import { Comp1Component } from './components/comp1.component';
@NgModule({
  declarations: [Comp1Component],
  exports: [Comp1Component]
})
export class LibModule {}
Reproduction steps

From the repo https://github.com/kepennar/ng-cli-lib-issue-repro

npm run build:lib npm run start:prod

Expected Behaviour

I would like ng-packagr to support correctly barrels files

Version Information

Output from: ng --version.

Angular CLI: 6.0.7
Node: 10.3.0
OS: linux x64
Angular: 6.0.3
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                            Version
------------------------------------------------------------
@angular-devkit/architect          0.6.7
@angular-devkit/build-angular      0.6.7
@angular-devkit/build-ng-packagr   0.6.7
@angular-devkit/build-optimizer    0.6.7
@angular-devkit/core               0.6.7
@angular-devkit/schematics         0.6.7
@angular/cli                       6.0.7
@ngtools/json-schema               1.1.0
@ngtools/webpack                   6.0.7
@schematics/angular                0.6.7
@schematics/update                 0.6.7
ng-packagr                         3.0.0
rxjs                               6.2.0
typescript                         2.7.2
webpack                            4.8.3

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 34
  • Comments: 31 (7 by maintainers)

Commits related to this issue

Most upvoted comments

When I change import to

import { Comp1Component } from './components/index';

I have this error during lib build:

BUILD ERROR
Cannot read property 'module' of undefined
TypeError: Cannot read property 'module' of undefined
    at MetadataBundler.convertSymbol (.../node_modules/@angular/compiler-cli/src/metadata/bundler.js:312:61)

@blemaire @deepthan Yes, we know. It literally says that direct imports work in the original ticket description:

If the component is directly imported from it’s own file. It’s work

The bug is that barrel imports (index.ts) don’t work, but they should.

To fix this, you must import the directive/component from the actual file into the module (not from an index.ts file)

Seems like something that should have been working long ago (barrel files are not exactly new) and there have been numerous tickets with 0 fix. More generally, it seems odd to go with TypeScript then be fine with a compiler that breaks with a pretty much meaningless error message whenever it encounters something like a barrel file or an extra comma.

I have a monorepo with 50+ modules and barrel files all over the place, adding /index everywhere is not exactly a solution, especially when some of those barrel files export hundreds of classes. And I assume I’m far from the only one with such a project size.

Can someone from the ng-packagr project give some hints on where (as in which project) the fix is needed so that someone can at least try to take a look and fix it? Should it be in ng-packagr, in angular/cli? There seems to have been a PR that may help a bit but didn’t quite fix the issue: https://github.com/angular/angular/pull/22856

@kepennar Importing it from where it is defined will fix it. It seems like ng-packagr has problems with barrel files.

I fixed my issue by migrating to angular/cli for my libraries

edit: Github removing @ mentions to repos