-1

I want my javascript to print a stack trace when I force close, so that I know where a function is hanging for instance. Is there a way to do this? I run the code with Node, specifically npx ts-node code.ts. I imagine hijacking sigint might work, but then the console.trace would be from the sigint block and not the original code -- I am stuck making progress here.

John Targaryen
  • 1,109
  • 1
  • 13
  • 29

2 Answers2

2

The Node runtime had the --trace-sigint argument added in May 2020 (as part of nodejs/node#29207 for this very purpose.

When running through npx, its --node-options flag can be used to pass this option on to the runtime like so:

npx --node-options='--trace-sigint' ts-node code.ts
esqew
  • 42,425
  • 27
  • 92
  • 132
  • Turns out it only backtraces to a library function at `at processTicksAndRejections (node:internal/process/task_queues:96:5)`, and not to the actual function of the main code that calls the failed library function. Any ideas? – John Targaryen Aug 09 '21 at 00:22
0

Use the option --trace-sigint.

The documentation states simply that this "Prints a stack trace on SIGINT."

Complete command:

npx --node-options='-trace-sigint' ts-node code.ts
Codebling
  • 10,764
  • 2
  • 38
  • 66