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

Bug Report or Feature Request (mark with an x)

- [x] bug report -> please search issues before submitting
- [ ] feature request


Angular CLI: 1.5.0-rc.5
Node: 8.8.1
OS: darwin x64
Angular: 5.0.0-rc.7
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, platform-server, router

@angular/cli: 1.5.0-rc.5
@angular-devkit/build-optimizer: 0.0.31
@angular-devkit/core: 0.0.20
@angular-devkit/schematics: 0.0.34
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.8.0-rc.5
@schematics/angular: 0.0.48
typescript: 2.4.2
webpack-bundle-analyzer: 2.9.0
webpack: 3.8.1

Repro steps.

in my package, anyway ng serve --ssl --public-host=//localhost:4200/sockjs-node/ --deploy-url=//localhost:4200/ --serve-path=/ make a trivial change to a template

The log given by the failure.

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

Desired functionality.

Successful compilation

Mention any other details that might be useful.

After the first build, everything works correctly. But a trivial change to a template or a change to whitspace in a ts file gives the error shown.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 10
  • Comments: 33 (7 by maintainers)

Most upvoted comments

Was there something reverted regarding this issue? I’m getting this error with 1.6.2

Have been struggling with this issue for a couple of days now and finally solved it after no other solution worked for me. Like in all the other cases I’ve read here and in other places, every other compilation would fail with the message “Cannot read property ‘text’ of undefined” and the stacktrace points to different functions in typescript.js. .

I’ve opened the file typescript.js and went to the function “transformCommonJSModule” that appears in the error’s stack trace. I’ve added console.log(“####”, node) to the beginning of the function, logging the function’s parameter, node. I then proceeded to recompile the project. When I got the compilation bug again, I went to the last appearance of “####” in the log and in the “node” object’s content there were several fields called “fileName”. They all pointed to a file called “something.service.factory.js”. The file something.service.ts contains an injectable service in my application.

I went to that file and checked what changes I made to it when the bug first appeared. Turns out I’ve added a method to this service and made it public and static. For some reason, the compiler didn’t like it half of the time. I removed the “static” notation and fixed all references to it and it solved the problem.

It seems this bug can be caused by different things. The process I described above can help narrow down what the cause is.

Good luck!

Easy fix for now is --no-aot.

Thanks, @shaizel for your suggestion it helped me track down what was causing this error for me.

For anyone like me coming to this and running a hybrid AngularJS and Angular application. Try removing the @types/angular dependency from your package.json

Getting the same in @angular/cli@1.7.3

get same error in angular-cli@1.7.2

#metoo @1.7.2

@filipesilva In that case, I’m closing this issue in favor of #8263. The original issue with “Cannot read property ‘text’…” on rebuild in AOT is fixed, and it looks like the other issue is where you are handling the problematic libraries. By the way, thanks for the kind words about BoardGameGeek!

Given the references to the other issues I assume you’re already thinking this is related to AOT. So just to make it explicit: the problem goes away when using ng serve --no-aot.

(Angular 5.0.0-rc.8, Angular CLI 1.5.0-rc.6.)