我一直读到Node中本机支持源映射。但我不明白在将错误打印到控制台时如何使用源映射。
我尝试过用--enable-source-maps
运行node,也尝试过source-map-support
包。但无济于事。控制台中的输出只显示经过转换的js代码,而不是ts源代码。
我做错了什么?
源代码:
// main.ts
const someError = new Error()
console.error(someError.stack)
控制台输出(来自VS代码(:
/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,
}
}
原来您需要将--enable-source-maps
放在文件参数的前面。
所以这是有效的:
node --enable-source-maps somescript.js
但这不会:
node somescript.js --enable-source-maps
🤦