【问题标题】:Printing error stack with source (from source map)使用源打印错误堆栈(来自源映射)
【发布时间】:2022-10-25 17:10:41
【问题描述】:

我一直在阅读 Node.js 原生支持源映射。但是我不明白在向控制台打印错误时如何使用源映射。

我试过用--enable-source-maps 运行节点,也试过source-map-support 包。但无济于事。控制台中的输出只显示了转译后的 js 代码,而不是 ts 源代码。

我究竟做错了什么?

源代码:

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

控制台输出(来自 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

这是我的 tsconfig.json:

// tsconfig.json
{
    "compilerOptions": {
        "target": "ES2022",
        "module": "commonjs",
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "sourceMap": true,
        "strict": true,
        "skipLibCheck": true,
        "outDir": "build",
        "noImplicitAny": false,
    }
}

【问题讨论】:

    标签: node.js typescript source-maps


    【解决方案1】:

    原来你需要--enable-source-maps在前文件参数。

    所以这有效:

    node --enable-source-maps somescript.js
    

    但这不会:

    node somescript.js --enable-source-maps
    

    ?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-15
      • 2013-03-03
      • 1970-01-01
      • 2016-11-16
      • 2019-06-22
      相关资源
      最近更新 更多