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

@filipesilva Do you have and suggestions as to how I could find the cause of the error. I have 2 projects I'm working on and only 1 has this issue, but the error message doesn't provide any clue as to the cause, so it's hard to create any repro that has the same issue as it could be pretty much anything.

dottodot avatar Jan 15 '18 10:01 dottodot

Hard to say, since the error seems deep into the typescript stack. Assuming you have a similar error stack to the one shown in the original report, I'd try adding some console logs to the TS functions related to module handling (like createRequireCall). That finding the variable there that shows the import might give you a good hint.

filipesilva avatar Jan 15 '18 10:01 filipesilva

OK thing I've found my issue. I had a server side service that had import { Request, Response } from 'express'; After removing that the error no longer occures

dottodot avatar Jan 15 '18 10:01 dottodot

if it's of any help, i'm getting this in a hybrid application and it seems like the plugin generates .ngfactory.ts files for non-angular modules. is that intended behavior? in these (non-angular) files, error happens for imports of this form import * as angular from 'angular'; (as some mentioned before). also if i add a null check in typescript compiler everything works out fine, each change appears correctly when browser reloads. so i'm wondering if the plugin is processing files that it shouldn't, just guessing though.

groetzi avatar Jan 15 '18 10:01 groetzi

@groetzi that isn't intended and seems like https://github.com/angular/angular/issues/20115. Can you tell me the name of the library that's getting factory files generated?

filipesilva avatar Jan 15 '18 12:01 filipesilva

@filipesilva it's not an external library file but one that is part of our project in this case an angularjs http interceptor. but i can see this error propagate through non-angularjs modules as well when removing the imports file by file. so e.g. a simple module with this import import * as XLSX from 'xlsx'; will run into the sourcefile undefined error with something like excelutils.ngfactory.ts.

console.log(importNode) in createRequireCall seems like the processed token is something like that text: '@types/xlsx/index', while the console.log(currentSourceFile) exposes this text: 'import * as i0 from \'@angular/core\';\ni0.ComponentFactory;\n', (with filename being ...ExcelUtils.ngfactory.ts).

i'm not completly sure if it's a configuration error though (i have basically no experience with angular2+ btw), but we're supposed to let angular webpack plugin handle all typescript files in the project right? and it will figure out for which files to generate factory code? our tsconfig is configured to include any .ts files within the src folder of our project but exclude node_modules.

groetzi avatar Jan 15 '18 13:01 groetzi

Yeah you shouldn't have to care about which files get a ngfactory or not, that's on our side. Could you setup a simple repro I can debug on please? Something like a ng new project, then add a file with no Angular with imports, and import it, so the error is shown. I can then use it to figure out where it's going wrong.

filipesilva avatar Jan 15 '18 13:01 filipesilva

ok. i couldnt reproduce this yet with a clean cli project, hope to find some time to dig deeper into this soon.

groetzi avatar Jan 15 '18 16:01 groetzi

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.

filipesilva avatar Jan 15 '18 17:01 filipesilva

As an additional data point, I am using allowSyntheticDefaultImports. Are other people seeing the error also using that?

wulfsberg avatar Jan 18 '18 19:01 wulfsberg

We've experienced this issue on Node 6 with aot flag and proxy config. I've upgraded to cli 1.6.5 node 8 and I am still getting same error. It seems that with angular cli 1.6.1 it worked fine.

SimonNodel-AI avatar Jan 23 '18 01:01 SimonNodel-AI

still existing in 1.7.0-beta.1

marvinosswald avatar Jan 24 '18 11:01 marvinosswald

@filipesilva please find a repro here: https://github.com/victornoel/repro-bug-angular-cli

victornoel avatar Feb 01 '18 08:02 victornoel

@victornoel thank you so much for the repro!

filipesilva avatar Feb 01 '18 09:02 filipesilva

So until a fix, workaround is downgrading to 1.6.1?

luchillo17 avatar Feb 09 '18 18:02 luchillo17

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.

filipesilva avatar Feb 13 '18 10:02 filipesilva

Tried to reproduce with the Angular CLI 6 and the repro in https://github.com/angular/angular-cli/issues/9036#issuecomment-362189011, and couldn't see the error anymore. This should be sorted now.

filipesilva avatar Sep 28 '18 09:09 filipesilva

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.

allout58 avatar Mar 19 '19 14:03 allout58

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

what is that related to express? I also got the error when building Angular universal?

chaoyangnz avatar Jul 22 '19 22:07 chaoyangnz

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.