Adrian Ber
Adrian Ber

Reputation: 21370

"Maximum call stack size exceeded" in Angular 9 ng build --prod for a library

I have a library that I'm able to compile with Angular 9. But if I change the flag to --prod, which practically makes "enableIvy": false then I get

[error] RangeError: Maximum call stack size exceeded

at Object.pathIsRelative (...\node_modules\typescript\lib\typescript.js:5778:34)

at tryLoadModuleUsingPathsIfEligible (...\node_modules\typescript\lib\typescript.js:28346:37)

at tryLoadModuleUsingOptionalResolutionSettings (...\node_modules\typescript\lib\typescript.js:28334:24)

at tryResolve (...\node_modules\typescript\lib\typescript.js:28479:28)

at ...\node_modules\typescript\lib\typescript.js:28471:69

at Object.forEach (...\node_modules\typescript\lib\typescript.js:309:30)

at nodeModuleNameResolverWorker (...\node_modules\typescript\lib\typescript.js:28471:25)

at nodeModuleNameResolver (...\node_modules\typescript\lib\typescript.js:28464:16)

at Object.resolveModuleName (...\node_modules\typescript\lib\typescript.js:28238:30)

at CompilerHostAdapter.getMetadataFor (...\node_modules@angular\compiler-cli\src\metadata\bundler.js:567:37)

I introduced a console debug message in function tryLoadModuleUsingPathsIfEligible(extensions, moduleName, loader, state) function and it seems to be a loop with approximately 20 moduleNames. I have to mention that I do NOT have any circular dependency warning if I compile without --prod flag.

Update: I fixed the circular dependencies and I have no more warnings of sort.

Upvotes: 13

Views: 23114

Answers (8)

Marcin Licznerski
Marcin Licznerski

Reputation: 380

In my case, with Angular 16, I've accidentally added a Component (non-standalone) to imports (instead of declarations):

@NgModule({
  imports: [
    CommonModule,
    TranslateModule,
    AuthModule,
    RouterModule,
    HeaderComponent, // <- invalid import of a Component
  ],
  declarations: [AvatarComponent],
  exports: [AvatarComponent],
  providers: [],
})
export class SharedHeaderModule {}

Additionally, sometimes re-running ng serve is required.

Upvotes: 11

Paul Klink
Paul Klink

Reputation: 76

Error occurred for me when the forwardRef line below somehow got into my ContentNgModule file.

imports: [
    CommonModule,
    forwardRef(() => ContentNgModule)
]

Upvotes: 0

Dinei
Dinei

Reputation: 5444

Facing the same issue, different cause: I was trying to import and export an Angular Component directly in a module (It was intended to be declared and exported by other module, and then this module would be imported+exported in the first one).

Upvotes: 1

Inesa Toroyan
Inesa Toroyan

Reputation: 31

First, remove node_modules and reinstall dependencies - this is a just in case solution. Generally, what happens is

  • Self Import in a module
  • Cycle dependency, cycle imports
  • recursive call with no termination

Be very careful to the 2 points in the beginning, as angular generally not showing that something is wrong in the imports. That may be a good reason for call stack size explosion. Hope it helps.

Upvotes: 2

Salahuddin Ahmed
Salahuddin Ahmed

Reputation: 5650

Try deleting unnecessary imports, if anything like this:

'A' module imports 'B' module and vice versa

Upvotes: 4

Walkance
Walkance

Reputation: 392

I had the same problem with angular 12.1.1, I had no circular dependenicies, the solutution was to change "enableIvy: false" to "compilationMode": "partial".

See angular docs.

Upvotes: 9

alydemah
alydemah

Reputation: 59

Been facing the same issue, in my case there was index.ts with invalid export

export * from './'

Fixed it and it build again with ng build

Upvotes: -1

LuJaks
LuJaks

Reputation: 1179

In the end, aber rm -rf node_modules I also needed to do npm cache clean --force - also I made sure I really had all dependencies updated. (Not sure which of those things did the trick)

Upvotes: 0

Related Questions