Ts-node: TypeError: Cannot read property 'parent' of undefined

Created on 22 Oct 2016  Â·  27Comments  Â·  Source: TypeStrong/ts-node

I reported it on the TypeScript repo as well. Seen here: https://github.com/Microsoft/TypeScript/issues/11703

Various different setup configuration and still same issue.

  at getOutput (node_modules\ts-node\src\index.ts:299:32)
    at node_modules\ts-node\src\index.ts:340:18
    at Object.compile (node_modules\ts-node\src\index.ts:473:19)
    at Module.m._compile (node_modules\ts-node\src\index.ts:403:44)
    at Module._extensions..js (module.js:579:10)
    at Object.require.extensions.(anonymous function) [as .ts] (node_modules\ts-node\src\index.ts:406:12)
    at Module.load (module.js:487:32)
bug external

All 27 comments

Can you please share your configuration? Are you using allowJs in tsconfig.json? What version of ts-node and node are you running? Anything else I could use to reproduce?

@blakeembrey Click on the link above to the issue reported on the TypeScript repo. Latest post there you see my configuration.

Regarding your questions. Here is my tsconfig.json configuration

{
    "compilerOptions": {
        "allowJs": false,
        "allowUnreachableCode": false,
        "forceConsistentCasingInFileNames": true,
        "declaration": false,
        "experimentalDecorators": true,
        "moduleResolution": "node",
        "target": "es6",
        "lib": ["es7"],
        "noEmitHelpers": true,
        "newLine": "LF",
        "noImplicitAny": true,
        "project": "src",
        "noLib": false,
        "removeComments": false,
        "pretty": true,
        "strictNullChecks": true,
        "stripInternal": true,
        "sourceMap": false,
        "inlineSourceMap": false,
        "suppressImplicitAnyIndexErrors": false,
        "outDir": "lib/",
        "types": ["mocha", "chai", "node"]
}}

I also tried running this on NodeJS v. 4 - 6. All versions. Same thing

@zubuzon Are you sure that you haven't got allowJs enabled? From the stack trace it looks like you're falling through from TypeScript -> JavaScript so that's either from a file in node_modules or allowJs. However, if it's from node_modules that isn't in line with the title - can you share the full stack-trace you're seeing?

Edit: Actually, that seems incorrect. I was just thrown off by the half a stack-trace. Could you still share the full stack-trace?

Perhaps I can enable a --debug-require flag so we can at least figure out when it's crashing?

@blakeembrey Different stack trace now. Here is the complete one:

TypeError: Cannot read property 'parent' of undefined
    at visitNode (node_modules\typescript\lib\typescript.js:13558:22)
    at visitEachNode (node_modules\typescript\lib\typescript.js:12943:30)
    at forEachChild (node_modules\typescript\lib\typescript.js:13315:24)
    at visitNode (node_modules\typescript\lib\typescript.js:13562:21)
    at visitNode (node_modules\typescript\lib\typescript.js:12931:20)
    at forEachChild (node_modules\typescript\lib\typescript.js:13313:21)
    at visitNode (node_modules\typescript\lib\typescript.js:13562:21)
    at visitEachNode (node_modules\typescript\lib\typescript.js:12943:30)
    at forEachChild (node_modules\typescript\lib\typescript.js:13299:24)
    at visitNode (node_modules\typescript\lib\typescript.js:13568:29)
    at visitEachNode (node_modules\typescript\lib\typescript.js:12943:30)
    at forEachChild (node_modules\typescript\lib\typescript.js:13103:24)
    at fixupParentReferences (node_modules\typescript\lib\typescript.js:13552:13)
    at parseSourceFileWorker (node_modules\typescript\lib\typescript.js:13525:17)
    at Object.parseSourceFile (node_modules\typescript\lib\typescript.js:13470:26)
    at Object.createSourceFile (node_modules\typescript\lib\typescript.js:13329:29)
    at createLanguageServiceSourceFile (node_modules\typescript\lib\typescript.js:74491:29)
    at Object.updateLanguageServiceSourceFile (node_modules\typescript\lib\typescript.js:74544:16)
    at acquireOrUpdateDocument (node_modules\typescript\lib\typescript.js:65898:43)
    at Object.updateDocumentWithKey (node_modules\typescript\lib\typescript.js:65877:20)
    at getOrCreateSourceFileByPath (node_modules\typescript\lib\typescript.js:74740:49)
    at Object.getOrCreateSourceFile [as getSourceFile] (node_modules\typescript\lib\typescript.js:74697:24)
    at findSourceFile (node_modules\typescript\lib\typescript.js:59696:29)
    at processImportedModules (node_modules\typescript\lib\typescript.js:59829:25)
    at findSourceFile (node_modules\typescript\lib\typescript.js:59724:17)
    at processImportedModules (node_modules\typescript\lib\typescript.js:59829:25)
    at findSourceFile (node_modules\typescript\lib\typescript.js:59724:17)
    at processSourceFile (node_modules\typescript\lib\typescript.js:59627:27)
    at processRootFile (node_modules\typescript\lib\typescript.js:59521:13)
    at node_modules\typescript\lib\typescript.js:58982:60
    at Object.forEach (node_modules\typescript\lib\typescript.js:1238:30)
    at Object.createProgram (node_modules\typescript\lib\typescript.js:58982:16)
    at synchronizeHostData (node_modules\typescript\lib\typescript.js:74675:33)
    at Object.getEmitOutput (node_modules\typescript\lib\typescript.js:74948:13)
    at getOutput (node_modules\ts-node\src\index.ts:299:32)
    at node_modules\ts-node\src\index.ts:340:18
    at Object.compile (node_modules\ts-node\src\index.ts:473:19)
    at Module.m._compile (node_modules\ts-node\src\index.ts:403:44)
    at Module._extensions..js (module.js:579:10)
    at Object.require.extensions.(anonymous function) [as .ts] (node_modules\ts-node\src\index.ts:406:12)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (src\utils\xxx.ts:2:1)
    at Module._compile (module.js:570:32)
    at Module.m._compile (node_modules\ts-node\src\index.ts:403:23)
    at Module._extensions..js (module.js:579:10)
    at Object.require.extensions.(anonymous function) [as .ts] (node_modules\ts-node\src\index.ts:406:12)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (test\specs\xxx.ts:1:1)
    at Module._compile (module.js:570:32)
    at Module.m._compile (node_modules\ts-node\src\index.ts:403:23)
    at Module._extensions..js (module.js:579:10)
    at Object.require.extensions.(anonymous function) [as .ts] (node_modules\ts-node\src\index.ts:406:12)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at node_modules\mocha\lib\mocha.js:222:27
    at Array.forEach (native)
    at Mocha.loadFiles (node_modules\mocha\lib\mocha.js:219:14)
    at Mocha.run (node_modules\mocha\lib\mocha.js:487:10)
    at loadAndRun (node_modules\mocha\bin\_mocha:415:22)
    at Object.<anonymous> (node_modules\mocha\bin\_mocha:432:3)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3

Have you tried running any of this without ts-node? Does tsc succeed?

I just pushed up a branch, https://github.com/TypeStrong/ts-node/tree/debug-log, if you want to test that locally and see where it's failing it may help. No guarantees though, but at least it'll give a sense of how many requires are happening and which one it fails on.

Can you also share your mocha.opts? Is there anything else you can share that might help me debug? Looking over the stack-trace again, it looks like ts-node never even starts - have you checked with tsc? How many files are we looking at in the project? Do you have node_modules linked within the project somewhere? Is there any symlinks I should know about?

Edit: Never starts in the sense that only ts-node has initiated TypeScript, but no actual require has made it meaning the runtime has never started and all this is the initial compile.

@blakeembrey Sorry late reply. If I downgrade TS version to 2.0.3 everything works as expected. And if I still are using latest TS nigthly build everything works as expected. If that helps.
The issue occur only when I run tests with latest TS v. _next_.

My mocha.opts is this:

--require ts-node/register
--ui bdd
--reporter spec
--bail
--full-trace
--check-leaks
--watch-extensions tsx,ts
--colors
--recursive
--timeout 5000

node_modules are excluded, and when I tried to track this down I was only running one test file with 50 lines of code. Similiar size of the source code.

I ran the ts-node you linked too. All tests pass there except one for the CLI:

1) should pipe intots-nodeand evaluate

But couldn't find a way to activate the logging

Ignore the tests. The logging was in the README, use TS_NODE_LOG_DEBUG=true as an environment variable. I'll try to replicate it soon, but it's definitely a lot easier when it's something like this to narrow down the issue and replicate it. If it's only one test, it should be easy enough to create a stand-alone repo that someone could checkout and debug. It certainly makes like for us maintainers 100x easier than going back on forth on project details 😄

Did you try running tsc on it yet by the way? Maybe you've found an issue with the TypeScript compiler? Hard to tell without seeing the file that's erroring. If it helps, you can always get in touch by email as well.

@blakeembrey if we could communicate outside GH it could make it easier. I can then grant access to part the project itself (_not for GH_) so you could use that to debug with. If I fail now trying to track it myself.

The TypeScript compiler isn't the issue here. The project compiles just fine.

@blakeembrey Okay. Follow this step-by-step

  1. Create a file named defineProperty.ts, and paste in this code:
/**
 * Define property
 *
 * @typedef {Object} defineProperty
 * @property {[any]} [obj]
 * @property {[string]} [prop]
 * @property {[val]} [val]
 */
function defineProperty(obj: any, prop: string, val: any): any {

    return Object.defineProperty(obj, prop, {
        configurable: true,
        enumerable: false,
        writable: true,
        value: val
    });
}
export default defineProperty;
  1. Create a new file that you namme dummy.ts, and paste in this code:
import defineProperty from './defineProperty';

function bar(): void {
    defineProperty('a', 'cached', true);
}

export default function foo (): void {};
  1. Create a test file and use this test code:
import foo from '../../src/dummy';

const expect = chai.expect;

describe('foo', function() {

 it('should export a function', function() {
     expect(typeof foo).to.eql('function');
 });

});

Run the test, and you will see what I see :)

_Note!_ TS latest nightly build is the TS version you need to use. If you use TS 2.0.3 it works without errors.

@blakeembrey I know what's causing this, and I didn't expect THAT !!

This will fail:

/**
 * Define property
 *
 * @typedef {any} defineProperty
 * @property {[any]} [obj]
 * @property {[string]} [prop]
 * @property {[val]} [val]
 */
function defineProperty(obj: any, prop: string, val: any): any {

    return Object.defineProperty(obj, prop, {
        configurable: true,
        enumerable: false,
        writable: true,
        value: val
    });
}
export default defineProperty;

THIS is not failing

/**
 * Define property
 */
function defineProperty(obj: any, prop: string, val: any): any {

    return Object.defineProperty(obj, prop, {
        configurable: true,
        enumerable: false,
        writable: true,
        value: val
    });
}
export default defineProperty;

Do you see it? Look at the differences between the comments!! Yep. That cause the whole thing to explode

@blakeembrey I video recorded this. Should I send it on an email?

Thanks for the debugging. I'll try running it now and seeing what happens. I'll get back to you, but you're always free to email me (the email is on my GitHub profile).

@blakeembrey Check your email. I sent the video there. To be honest I have never experienced this things before!

I just tried replicating it according to the comments, but it still isn't failing. I'm using:

→ ts-node --version
ts-node v1.6.0
node v6.8.0
→ tsc --version
Version 2.1.0-dev.20161022

Are those the same versions you can reproduce on?

image

I can create a test repo and add you to it if that'll help you create a reproduction?

Yes. It's the same. Did you see the video in the email I sent you ?

Actually, I got it working once I replicated the rest of the project in the TypeScript issue. Narrowing it down now.

Edit: Yes, I did.

So you see the bug?

That is literally insane. I'll create a repro repo that @mhegazy can review.

Here's the repro I was using to verify - https://github.com/blakeembrey/ts-and-ts-node-compile-bug-224. If you npm install and then run npm test, it should repro.

I looked at it, but with all this comments in the code I'm sure it will break!

@blakeembrey Much smaller repro explained here.

@blakeembrey I'm closing this. The issue have been fixed by the TypeScript team.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dakom picture dakom  Â·  3Comments

huan picture huan  Â·  3Comments

sanex3339 picture sanex3339  Â·  4Comments

OliverJAsh picture OliverJAsh  Â·  3Comments

mattdell picture mattdell  Â·  4Comments