Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot read property 'module' of undefined while building angular lib

I am converting an angular app to angular lib. I generated angular lib project and slowly moving the modules. I now get the following error while building the library.

ng build lib

Error

Cannot read property 'module' of undefined
TypeError: Cannot read property 'module' of undefined
    at MetadataBundler.convertSymbol (C:\Code\abc\client\node_modules\@angular\compiler-cli\src\metadata\bundler.js:312:61)
    at createReference (C:\Code\abc\client\node_modules\@angular\compiler-cli\src\metadata\bundler.js:438:27)
    at MetadataBundler.convertReference (C:\Code\abc\client\node_modules\@angular\compiler-cli\src\metadata\bundler.js:486:28)
    at MetadataBundler.convertExpression (C:\Code\abc\client\node_modules\@angular\compiler-cli\src\metadata\bundler.js:415:37)
    at C:\Code\abc\client\node_modules\@angular\compiler-cli\src\metadata\bundler.js:364:79
    at Array.map (<anonymous>)
    at MetadataBundler.convertMember (C:\Code\abc\client\node_modules\@angular\compiler-cli\src\metadata\bundler.js:364:47)
    at C:\Code\abc\client\node_modules\@angular\compiler-cli\src\metadata\bundler.js:349:70
    at Array.map (<anonymous>)
    at MetadataBundler.convertMembers (C:\Code\abc\client\node_modules\@angular\compiler-cli\src\metadata\bundler.js:349:38)

Typically this error is caused when trying to register dependencies incorrectly in the module. How can I find, which part of my configuration makes this exception?

It works fine in angular app.

like image 921
Abhishek Avatar asked Apr 14 '19 06:04

Abhishek


Video Answer


2 Answers

If angular decides to fail on aot compilation, you could try to go into node_modules\@angular\compiler-cli\src\metadata\bundler.js and do some console logging in that file. I would start from placing console.log in convertSymbol function. enter image description here That should provide you with some information about the file path which angular cli used before falling on module compilation. For example in my case, it was the re-export of the constant from the barrel which broke the compilation enter image description here So finding all the usages of TWO_FA_INITIAL_STATE, and directly importing TWO_FA_INITIAL_STATE, fixed that particular problem.

like image 151
Mike Avatar answered Sep 28 '22 19:09

Mike


I've just hit this problem with the following file structure:

components/component-n/component-n.component.ts
components/index.ts
my-module.ts

Where components/index.ts contains:

export { ComponentN } from './component-n/component-n.component.ts';

And my-module.ts contains:

import { ComponentN } from './components';

@NgModule({
  // ...
  declarations: [
    ComponentN
  ]
})
export class MyModule {}

Turns out the Angular compiler can't cope with this and throws the "Cannot read property 'module' of undefined" error.

Changing my-module.ts to import directly from components/component-n/component-n.component.ts instead, and deleting the index.ts, fixed it for me.

like image 25
daiscog Avatar answered Sep 28 '22 18:09

daiscog