Search code examples
javascripttracestack-tracesigint

is there a way to print the stacktrace of the current JS line when I ctrl+c in terminal?


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.


Solution

  • 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