Printing error stack with source (from source map)
Asked Answered
C

1

7

I keep reading that source maps are natively supported in Node. But I don't understand how I can use the source maps when printing an error to the console.

I have tried running node with --enable-source-maps, and I also tried the source-map-support package. But to no avail. The output in the console is only showing the transpiled js code, not the ts source code.

What am I doing wrong?

Source code:

// main.ts
const someError = new Error()
console.error(someError.stack)

Console output (from VS Code):

/home/birger/.nvm/versions/node/v16.16.0/bin/node ./build/main.js -r source-map-support/register
Error
    at Object.<anonymous> (/home/birger/someproject/build/main.js:8:19)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

Here is my tsconfig.json:

// tsconfig.json
{
    "compilerOptions": {
        "target": "ES2022",
        "module": "commonjs",
        "sourceMap": true,
        "strict": true,
        "outDir": "build",
    }
}
Cytherea answered 17/8, 2022 at 9:46 Comment(0)
C
18

Turns out you need to have the --enable-source-maps in front of the file argument.

So this works:

node --enable-source-maps somescript.js

But this won't:

node somescript.js --enable-source-maps

🤦

Cytherea answered 25/10, 2022 at 9:10 Comment(2)
This needs a billion upvotes.Exaction
This comment needs a billion upvotes lolFinstad

© 2022 - 2024 — McMap. All rights reserved.