Deleter
Deleter

Reputation: 800

Obtaining "Invalid string length" while running ng build of a library

In Angular (see spec below), trying to generate a large library, thus running ng build <project>, we obtain

❌  Generating "fesm2015"

Invalid string length

Indeed, fesm2020 build succeeded.

The dist sourcemap for fesm2020 reached 32MB, while the fesm2015 sourcemap reached 33MB (.mjs.map file).

The entire source code is about 200 components distributed in 1 single library.

Reproduction

Error

Invalid string length after Generating fesm2015

Environment

_                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/


Angular CLI: 14.2.6
Node: 18.15.0 (Unsupported)
Package Manager: npm 9.5.0
OS: win32 x64

Angular: 14.2.6
... animations, cli, common, compiler, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1402.6
@angular-devkit/build-angular   14.2.6
@angular-devkit/core            14.2.6
@angular-devkit/schematics      14.2.6
@angular/compiler-cli           14.3.0
@schematics/angular             14.2.6
ng-packagr                      14.0.1
rxjs                            6.5.4
typescript                      4.7.3

Is the any suggestion that we can adopt, even something to get a clearer view of what's happening (no verbose error is printed). We'd like to avoid splitting the code into different libraries even if it seems to be a problem of source code size/length.

Upvotes: 1

Views: 530

Answers (2)

Nutan Gwari
Nutan Gwari

Reputation: 274

Solution:

In angular.json set cli.cache.enabled to false

cli: {
   cache: {
     enabled: false
   }
}

Explanation:

Error is related to JSON.stringify method when string length is over the accepted range.

angular v13 onwards cache is enabled by default. ng-packagr uses rollup.js to stringify cache json and throws this error.

as @vincenz-manto mentioned, this is fixed in v16 because ng-packagr switched to esbuild instead of rollup.js. In order to fix it for angular v13-v15, is to disable the ng cache.

Cheers!

Upvotes: 1

Deleter
Deleter

Reputation: 800

The solution appeared to be upgrading to Angular 16+, while splitting large codes into different library entry points.

This should no longer be a problem with version 16+.

Upvotes: 0

Related Questions