6

I wish I could get stack traces to work in TypeScript. I only seem to see the bottommost function name. I'm using Node.js v12.4.0 on Windows 10 (1803).

This is the code:

async function thrower() {
  throw new Error("test");
}

async function level1() {
  return await thrower();
}

async function level2() {
  return await level1();
}

async function level3() {
  return await level2();
}

async function main() {
  try {
    await level3();
  } catch(err) {
    console.warn("main error", err);
  }
}

console.log("node version", process.version);

main().then(() => {
  console.log("all done " + __filename);
}).catch((err) => {
  console.error("Something went wrong in here :(", __filename, err);
})

The resulting stack trace that does not mention level1 or level2 or level3:

ts-node test-stack.ts
node version v12.4.0
main error Error: test
    at D:\dev\server\test-stack.ts:2:9
    at step (D:\dev\server\test-stack.ts:31:23)
    at Object.next (D:\dev\server\test-stack.ts:12:53)
    at D:\dev\server\test-stack.ts:6:71
    at new Promise (<anonymous>)
    at __awaiter (D:\dev\server\test-stack.ts:2:12)
    at thrower (D:\dev\server\test-stack.ts:37:12)
    at D:\dev\server\test-stack.ts:6:16
    at step (D:\dev\server\test-stack.ts:31:23)
    at Object.next (D:\dev\server\test-stack.ts:12:53)
all done D:\dev\server\test-stack.ts
1

1 Answer 1

8

After some research and noticing the __awaiter, I decided to inspect what's TypeScript targeting. That was my problem.

Here's my bad tsconfig.json:

{
    "compilerOptions": {
      "experimentalDecorators": true,
      "emitDecoratorMetadata": true,
      "downlevelIteration": true,
    },
    "include": [
        "server/**/*", "tests"
    ],
    "exclude": [
        "node_modules",
        "**/*.spec.ts"
    ]
}

And this "target": "es2018" fixed it:

{
    "compilerOptions": {
      "experimentalDecorators": true,
      "emitDecoratorMetadata": true,
      "downlevelIteration": true,
      "target": "es2018"
    },
    "include": [
        "server/**/*", "tests"
    ],
    "exclude": [
        "node_modules",
        "**/*.spec.ts"
    ]
}

Resulting in this stack trace:

ts-node test-stack.ts
node version v12.4.0
main error Error: test
    at thrower (D:\dev\server\test-stack.ts:2:9)
    at level1 (D:\dev\server\test-stack.ts:6:16)
    at level2 (D:\dev\server\test-stack.ts:10:16)
    at level3 (D:\dev\server\test-stack.ts:14:16)
    at main (D:\dev\server\test-stack.ts:19:11)
    at Object.<anonymous> (D:\dev\server\test-stack.ts:27:1)
    at Module._compile (internal/modules/cjs/loader.js:774:30)
    at Module.m._compile (C:\Users\yuv\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:439:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:785:10)
    at Object.require.extensions.<computed> [as .ts] (C:\Users\yuv\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:442:12)
all done D:\dev\server\test-stack.ts
Sign up to request clarification or add additional context in comments.

4 Comments

Just remember to use a different production target if you have to target older environments.
Note I'm able to get the proper stack track when targeting "es2017". This is important for my use case as using the "es2018" target seems to make the Terser plugin of Webpack very unhappy.
either this or nodejs version 16 helped. I am using ts-node.
@Riki137 that alone didn't add stack traces, though i tried it first. I'm not sure if it made a difference for me or if only the target of es2018 did it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.