Cannot read property 'module' of undefined while building angular lib
Asked Answered
D

4

9

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.

Debatable answered 14/4, 2019 at 6:39 Comment(6)
Did you use ng generate library my-lib first?Immobility
yes. I generated the library first and it was building fine.Debatable
can you try the --watch flag?Immobility
Also try the --buildEventLog=buildEventLog optionImmobility
--buildEventLog=buildEventLog doesn't provide any helpful information.Debatable
need to see your modules really. do you maybe have inter-dependencies between library modules?Eroto
E
22

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.

Esquiline answered 9/9, 2019 at 21:14 Comment(1)
This was really helpful. In my case, I had an API module that was being generated from Swagger, but that I wasn't importing into my component module.Cembalo
U
3

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.

Unprofitable answered 21/5, 2021 at 16:7 Comment(0)
E
1

This happened to me when I forgot to export a particular file in my public-api.ts. The file in question was basically a module root, simply re-exporting other files from the module.

When I tried to import a symbol from said file, it would give me this error (but not even a call stack).

It's really quite strange, but adding said file to public-api.ts fixed the issue (I say it's strange because I was not importing above mentioned symbol from public-api.ts in the first place)

Emperor answered 28/9, 2020 at 17:48 Comment(0)
H
0

for a library project make sure you export your classes in the public-api.ts file

export * from './lib/classes/index';

Howells answered 14/12, 2023 at 3:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.