angular: TypeError: Cannot read property 'text' of undefined - Error: NGCC failed.

๐Ÿž Bug report

Command (mark with an x)

  • new
  • build
  • serve
  • test
  • e2e
  • generate
  • add
  • update
  • lint
  • extract-i18n
  • run
  • config
  • help
  • version
  • doc

Is this a regression?

Yes, the previous version in which this bug was not present was: 12.2.10

Description

After upgrading angular from version 12.2.10 to 12.2.11 or 12.2.12, I can't build anymore my project.

๐Ÿ”ฌ Minimal Reproduction

I attached a repo-app, under the project folder, run the follwoing to reproduce the error:


npm i
ng build

๐Ÿ”ฅ Exception or Error




- Generating browser application bundles (phase: setup)...TypeError: Cannot read property 'text' of undefined
    at NodeObject.getText (C:\Users\user\git\angular\repro-app\node_modules\typescript\lib\typescript.js:148615:31)
    at getRequiredModulePath (C:\Users\user\git\angular\repro-app\node_modules\@angular\compiler-cli\ngcc\src\host\umd_host.js:519:99)
    at Object.getImportsOfUmdModule (C:\Users\user\git\angular\repro-app\node_modules\@angular\compiler-cli\ngcc\src\host\umd_host.js:510:23)
    at UmdDependencyHost.extractImports (C:\Users\user\git\angular\repro-app\node_modules\@angular\compiler-cli\ngcc\src\dependencies\umd_dependency_host.js:43:54)
    at UmdDependencyHost.DependencyHostBase.recursivelyCollectDependencies (C:\Users\user\git\angular\repro-app\node_modules\@angular\compiler-cli\ngcc\src\dependencies\dependency_host.js:85:32)
    at UmdDependencyHost.DependencyHostBase.collectDependencies (C:\Users\user\git\angular\repro-app\node_modules\@angular\compiler-cli\ngcc\src\dependencies\dependency_host.js:38:22)
    at DependencyResolver.getEntryPointWithDependencies (C:\Users\user\git\angular\repro-app\node_modules\@angular\compiler-cli\ngcc\src\dependencies\dependency_resolver.js:75:22)
    at EntryPointCollector.walkDirectoryForPackages (C:\Users\user\git\angular\repro-app\node_modules\@angular\compiler-cli\ngcc\src\entry_point_finder\entry_point_collector.js:47:52)
    at EntryPointCollector.walkDirectoryForPackages (C:\Users\user\git\angular\repro-app\node_modules\@angular\compiler-cli\ngcc\src\entry_point_finder\entry_point_collector.js:75:103)
    at C:\Users\user\git\angular\repro-app\node_modules\@angular\compiler-cli\ngcc\src\entry_point_finder\program_based_entry_point_finder.js:124:100
C:\Users\user\git\angular\repro-app\node_modules\@ngtools\webpack\src\ngcc_processor.js:139
            throw new Error(errorMessage + `NGCC failed${errorMessage ? ', see above' : ''}.`);
            ^

Error: NGCC failed.
    at NgccProcessor.process (C:\Users\user\git\angular\repro-app\node_modules\@ngtools\webpack\src\ngcc_processor.js:139:19)
    at C:\Users\user\git\angular\repro-app\node_modules\@ngtools\webpack\src\ivy\plugin.js:129:27
    at Hook.eval [as call] (eval at create (C:\Users\user\git\angular\repro-app\node_modules\tapable\lib\HookCodeFactory.js:19:10), <anonymous>:20:1)
    at Hook.CALL_DELEGATE [as _call] (C:\Users\user\git\angular\repro-app\node_modules\tapable\lib\Hook.js:14:14)
    at Compiler.newCompilation (C:\Users\user\git\angular\repro-app\node_modules\webpack\lib\Compiler.js:1043:30)
    at C:\Users\user\git\angular\repro-app\node_modules\webpack\lib\Compiler.js:1088:29
    at Hook.eval [as callAsync] (eval at create (C:\Users\user\git\angular\repro-app\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:22:1)
    at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (C:\Users\user\git\angular\repro-app\node_modules\tapable\lib\Hook.js:18:14)
    at Compiler.compile (C:\Users\user\git\angular\repro-app\node_modules\webpack\lib\Compiler.js:1083:28)
    at C:\Users\user\git\angular\repro-app\node_modules\webpack\lib\Watching.js:200:19

๐ŸŒ Your Environment




Angular CLI: 12.2.12
Node: 14.18.1
Package Manager: npm 6.14.15
OS: win32 x64

Angular: 12.2.12
... animations, cdk, cli, common, compiler, compiler-cli, core
... forms, google-maps, language-service, localize, material
... platform-browser, platform-browser-dynamic, platform-server
... router, service-worker

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.1200.5
@angular-devkit/build-angular     12.2.12
@angular-devkit/build-optimizer   0.1200.5
@angular-devkit/core              12.0.5
@angular-devkit/schematics        12.2.12
@nguniversal/builders             12.1.3
@nguniversal/express-engine       12.1.3
@schematics/angular               12.2.12
ng-packagr                        12.2.5
rxjs                              6.6.7
typescript                        4.2.4

Anything else relevant?

Kindly check below a zip of an angular-cli project to reproduce the error: repro-app.zip

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 5
  • Comments: 19 (7 by maintainers)

Commits related to this issue

Most upvoted comments

This fix has been merged and will be included in the next v13 release (13.0.3).

@PraveenFrancis, typically we wouldnโ€™t backport this to v12 (which is an LTS version) based on our release policy, but since it was an easy/clean backport and we are releasing an LTS version to fix some other issues/regressions, I created #44309. So, the fix will eventually land to the v12 branch, but not sure when the next v12 release will be (could be in a month or so).

Youโ€™re right, forget to update the compiler-cli to 13.0.3 too. My fault, now it works!

Thx for checking and reporting back, @4kparadise ๐Ÿ‘ The fix has indeed landed on the 13.0.x branch (you can see the commit here: bf5f734e9c38bfcc43688410e70d741ff19b9acb). And I do see the fix in the 13.0.3 code.

Could you create a new issue, @4kparadise, and share a minimal reproduction? ๐Ÿ™

Actually, after looking into it a bit more, this turns out to be a different issue:

  • The error that is printed out originates from this line of the getRequiredModulePath() helper (so, itโ€™s an error caused while we are trying to throw a different error).
  • The error is caused by the fact that wrapperFn.body.getText() throws.
  • Internally, getText() tries to retrieve the ts.SourceFile of the corresponding node (here wrapperFn.body) and extract the text corresponding to the node.
  • In order to retrieve the ts.SourceFile, TypeScript traverses through the nodeโ€™s ancestors until it find a ts.SourceFile node (or until there is no parent):
  • In this case, retrieving the ts.SourceFile fails (i.e. return undefined), because wrapperFn.body.parent is not defined.
  • The reason .parent is not defined is that we create the source file with the setParentNodes argument set to false (i.e. tell TypeScript not to set parent nodes).
  • Once we fix this issue, we get to see the actual error that we were trying to print originally.
  • The actual problem is that wrapperFn.body.statements[0] is not an expression statement.
  • Typically, we expect the first (and only) statement of the UMD wrapper function body to be in the following form:
    typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('...'), ...) :
    typeof define === 'function' && define.amd ? define(['exports', '...', ...], factory) :
    (factory((global['...'] = {}), ...));
    
  • However, here, the UMD bundle of ng2-select@2.0.0 (which is the package that causes the error) looks like this:
    (function webpackUniversalModuleDefinition(root, factory) {
      if(typeof exports === 'object' && typeof module === 'object')
        module.exports = factory(...);
      else if(typeof define === 'function' && define.amd)
        define([...], factory);
      else if(typeof exports === 'object')
        exports["..."] = factory(...);
      else
        root["..."] = factory(...);
    })(...);
    

So, to fix this, we should update the UmdHost and associated helpers to cope with that additional UMD wrapper function body format that is emitted by Webpack.