angular-cli: ERROR in : TypeError: Cannot read property 'text' of undefined

Versions

Angular CLI: 1.6.3
Node: 6.11.4
OS: win32 x64
Angular: 5.1.2
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... platform-server, router

@angular/cli: 1.6.3
@angular/http: 4.4.6
@angular-devkit/build-optimizer: 0.0.36
@angular-devkit/core: 0.0.22
@angular-devkit/schematics: 0.0.42
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.9.3
@schematics/angular: 0.1.11
@schematics/schematics: 0.0.11
typescript: 2.4.2
webpack: 3.10.0

Repro steps

  1. npm start (compiles fine)
  2. edit and save a file
  3. The following error when it tries to re-compile

Observed behavior


ERROR in : TypeError: Cannot read property 'text' of undefined
    at getErrorSpanForNode (/usr/local/src/angular/node_modules/typescript/lib/typescript.js:7304:39)
    at createDiagnosticForNodeInSourceFile (/usr/local/src/angular/node_modules/typescript/lib/typescript.js:7229:20)
    at Object.createDiagnosticForNode (/usr/local/src/angular/node_modules/typescript/lib/typescript.js:7225:16)
    at error (/usr/local/src/angular/node_modules/typescript/lib/typescript.js:27167:22)
    at resolveExternalModule (/usr/local/src/angular/node_modules/typescript/lib/typescript.js:28226:17)
    at resolveExternalModuleNameWorker (/usr/local/src/angular/node_modules/typescript/lib/typescript.js:28213:20)
    at Object.getExternalModuleFileFromDeclaration (/usr/local/src/angular/node_modules/typescript/lib/typescript.js:47978:32)
    at tryGetModuleNameFromDeclaration (/usr/local/src/angular/node_modules/typescript/lib/typescript.js:15517:50)
    at Object.getExternalModuleNameLiteral (/usr/local/src/angular/node_modules/typescript/lib/typescript.js:15481:20)
    at createRequireCall (/usr/local/src/angular/node_modules/typescript/lib/typescript.js:61248:33)
    at visitExportDeclaration (/usr/local/src/angular/node_modules/typescript/lib/typescript.js:61310:45)
    at sourceElementVisitor (/usr/local/src/angular/node_modules/typescript/lib/typescript.js:61091:28)
    at Object.visitNodes (/usr/local/src/angular/node_modules/typescript/lib/typescript.js:49280:48)
    at transformCommonJSModule (/usr/local/src/angular/node_modules/typescript/lib/typescript.js:60834:40)
    at transformSourceFile (/usr/local/src/angular/node_modules/typescript/lib/typescript.js:60808:27)
    at /usr/local/src/angular/node_modules/typescript/lib/typescript.js:2492:86

Desired behavior

Have a non-failing build

Mention any other details that might be useful (optional)

I see this issue was a problem previously in #8216, and it looks like a few other people are also having this problem more recently, although I don’t see an issue for it (if there is one, sorry for the dupe).

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 30
  • Comments: 40 (9 by maintainers)

Commits related to this issue

Most upvoted comments

Hi all,

I looked into this yesterday and completely solving this problem is harder than it looks. The symptoms shown here are partial due to a performance optimization we have in ng serve where we set module: "commonjs" for typescript complication.

This greatly increases rebuild performance due to some limitations of Webpack 3. On the next major version of Angular CLI we will update to Webpack 4 which does not need this workaround. This should reduce the problem, but not eliminate it completely.

I’ve opened an issue in Angular itself https://github.com/angular/angular/issues/22193 for further investigation. It has some more details about the problem.

Just to add to this - I have also experienced this issue, but it is when using ng build watch with --aot flag enabled. It seems to fail every other time (i.e. once every two compiles).

I can confirm it started happening on 1.6.3 for me aswell using ng serve --aot on every other build.

If you are upgrading a project from an older version of Angular, make sure you update your tsconfig.json file to have module: es2015 or you might still be using CommonJS modules and have this issue.

@victornoel thank you so much for the repro!

@maximedupre I couldn’t find any imports that are using this style

import Tone from 'tone'; 

For my universal app I am using this

import * as express from 'express';
import { Request } from 'express';

I am still getting the error on same express type library.

For my app this error was being thrown when trying to import @types/redux-actions/index. My guess is that some of the aot generated code is directly importing that type file (since I’m not manually importing that type anywhere in my app code).

Unhelpful workaround: If you manually edit the getErrorSpanForNode function in typescript.js inside node_modules you can bypass the error without any negative consequences.

Add this before the error line:

if (!sourceFile) {
  return ts.createTextSpan(0, 0);
}

Of course, you would have to re-add this any time you ran an npm install and it won’t scale to multiple team members.

@isaacplmann Indeed, ng serve --prod works for me while ng serve --aot produces the error every other compile. Good (and rather strange) catch. The fact that it seems to be triggered by the OpenLayers import for me and the stack trace includes transformCommonJSModule looks mightily suspicious, but I can’t provoke a simple repro.

If I take off --aot=true the build works without errors. Also, ng build --prod works fine.

still existing in 1.7.0-beta.1

Thanks @groetzi, even if you don’t get manage to get a full repro it still helps if you tell us the stuff you tried that didn’t work. That might put someone else on the track for a repro.