angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

ERROR in : TypeError: Cannot read property 'text' of undefined

Open dalla777 opened this issue 8 years ago • 40 comments

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).

dalla777 avatar Dec 29 '17 22:12 dalla777

@dalla777 thanks for reopening this issue and happy new year 😀 I have this issue with ng serve -pc proxy.conf.local.json -ssl --env=test --aot As this also seams to be a problem with normal npm start I guess the options doesn't matter. But as this seams to be related to typescript I can confirm, that this error also occurs with typescript 2.5.3. While I looked this up, I discovered the following.

├─┬ @angular/[email protected]
│ └─┬ @angular-devkit/[email protected]
│   └── [email protected] 
└── [email protected]  

So it looks like build-optimizer might use another version of typescript?

DaSchTour avatar Jan 02 '18 07:01 DaSchTour

Heya, I tried to reproduce this with a new project by editing src/app/app.component.ts and did not see the error you describe:

kamik@T460p MINGW64 /d/sandbox/master-project (master)
$ npm start

> [email protected] start D:\sandbox\master-project
> ng serve

** NG Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **
Date: 2018-01-02T09:59:02.431Z
Hash: fd739962ff633c8914c5
Time: 7537ms
chunk {inline} inline.bundle.js (inline) 5.79 kB [entry] [rendered]
chunk {main} main.bundle.js (main) 20.4 kB [initial] [rendered]
chunk {polyfills} polyfills.bundle.js (polyfills) 549 kB [initial] [rendered]
chunk {styles} styles.bundle.js (styles) 33.7 kB [initial] [rendered]
chunk {vendor} vendor.bundle.js (vendor) 7.14 MB [initial] [rendered]

webpack: Compiled successfully.
webpack: Compiling...
Date: 2018-01-02T09:59:16.858Z
Hash: 7669a199f38283c7d515
Time: 195ms
chunk {inline} inline.bundle.js (inline) 5.79 kB [entry]
chunk {main} main.bundle.js (main) 20.4 kB [initial] [rendered]
chunk {polyfills} polyfills.bundle.js (polyfills) 549 kB [initial]
chunk {styles} styles.bundle.js (styles) 33.7 kB [initial]
chunk {vendor} vendor.bundle.js (vendor) 7.14 MB [initial]

webpack: Compiled successfully.
webpack: Compiling...
Date: 2018-01-02T09:59:19.018Z
Hash: fd739962ff633c8914c5
Time: 124ms
chunk {inline} inline.bundle.js (inline) 5.79 kB [entry]
chunk {main} main.bundle.js (main) 20.4 kB [initial] [rendered]
chunk {polyfills} polyfills.bundle.js (polyfills) 549 kB [initial]
chunk {styles} styles.bundle.js (styles) 33.7 kB [initial]
chunk {vendor} vendor.bundle.js (vendor) 7.14 MB [initial]

webpack: Compiled successfully.

This might be related to your project proper. Can you please upload a reproduction that I can follow to see this happen? Without a reproduction we just can't investigate the issue.

@DaSchTour Build Optimizer uses a different version of TypeScript, yes. That is intended.

filipesilva avatar Jan 02 '18 10:01 filipesilva

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).

LukeBillo avatar Jan 02 '18 11:01 LukeBillo

I get what @LukeBillo describes consistently in one project, but it is a fairly complicated one consisting of several modules. I've been doing some divide-and-conquer imports to try to figure out where it happens, and it seems to be when I import a module which exports a file which in turn import * as ol from 'openlayers'; Whether it's OpenLayers, the import as notation or something else, have yet to figure out.

In any case, it only happens when I ng serve --aot, and consistently on exactly every other compile, which makes it more than a little puzzling.

wulfsberg avatar Jan 02 '18 13:01 wulfsberg

I'm getting this error too. 1.6.1 works fine, but 1.6.2 and 1.6.3 fail. I'm also using a --proxy-config and --aot=true. Working on steps to reproduce.

isaacplmann avatar Jan 02 '18 14:01 isaacplmann

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

isaacplmann avatar Jan 02 '18 14:01 isaacplmann

@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.

wulfsberg avatar Jan 02 '18 14:01 wulfsberg

The stack trace for me also includes transformCommonJSModule, if that's helpful. I'm not using OpenLayers, but it's certainly possible one of the (many) npm packages installed in the project I'm working on may use it.

LukeBillo avatar Jan 02 '18 14:01 LukeBillo

It is rather unlikely that you're using OpenLayers without knowing it, but I suspect it's more about using CommonJS/UMD modules in general.

Is the "Cannot read property 'text' of undefined" actually an internal TypeScript error, crashing as it is trying to tell us the actual error, akin to https://github.com/Microsoft/TypeScript/issues/17925 ?

wulfsberg avatar Jan 02 '18 14:01 wulfsberg

Well I was hoping to come back from holiday and have it magically fixed itself, sadly this was not the case.

@filipesilva I installed a new project and copied over my package file to see if I could replicate the issue, but I wasn't getting the error, so I'm not sure how to get you replication steps :\

I can also confirm that turning off aot stops the error.

dalla777 avatar Jan 02 '18 17:01 dalla777

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 avatar Jan 02 '18 18:01 isaacplmann

I am also experiencing similar issue --- found that @isaacplmann solution stopped issue when I debugged into typescript.js I noticed that after the first change the node value was

TokenObject {
  pos: -1,
  end: -1,
  flags: 8,
  parent: undefined,
  kind: 9,
  text: '@types/express/index',
  modifierFlagsCache: 536870912,
  transformFlags: 536870912 }

I don't see node_modules/@types/express/index.js file I only see index.d.ts file

earlyster avatar Jan 02 '18 20:01 earlyster

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

xban1x avatar Jan 04 '18 09:01 xban1x

This might be a bug for all CommonJS modules.

import Tone from 'tone';

Tone is undefined.

maximedupre avatar Jan 08 '18 16:01 maximedupre

I started seeing this too when my app auto rebuilds on a change, but on a second save I don't get the error.

dottodot avatar Jan 10 '18 10:01 dottodot

Changing

import Tone from 'tone';

To

import * as Tone from 'tone';

Fixes the problem for me. Solution was found in #9058 .

maximedupre avatar Jan 10 '18 14:01 maximedupre

I was able to fix the issue in my app by removing all references to redux-actions and using class actions the same way the ngrx example-app does. Now I'm on the latest cli again.

isaacplmann avatar Jan 10 '18 16:01 isaacplmann

@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.

earlyster avatar Jan 11 '18 00:01 earlyster

downgrade to 1.6.1 fixed it for me, for now

marvinosswald avatar Jan 13 '18 12:01 marvinosswald

There's is a problem with the CommonJS like @maximedupre brought up, it's being tracked in https://github.com/angular/angular-cli/issues/9058 though and the error message shouldn't be the same as reported here (TypeError: Cannot read property 'text' of undefined at getErrorSpanForNode.), and it shouldn't be related to having --aot on or not.

I still need a reproduction of this issue to investigate though. Anyone had success in doing a minimal repro?

filipesilva avatar Jan 15 '18 10:01 filipesilva