我希望我可以在Typescript中使用堆栈跟踪。我似乎只看到最底部的函数名称。我正在Windows 10(1803)上使用Node.js v12.4.0。
这是代码:
async function thrower() {
throw new Error("test");
}
async function level1() {
return await thrower();
}
async function level2() {
return await level1();
}
async function level3() {
return await level2();
}
async function main() {
try {
await level3();
} catch(err) {
console.warn("main error", err);
}
}
console.log("node version", process.version);
main().then(() => {
console.log("all done " + __filename);
}).catch((err) => {
console.error("Something went wrong in here :(", __filename, err);
})
生成的堆栈跟踪未提及level1
或level2
或level3
:
ts-node test-stack.ts
node version v12.4.0
main error Error: test
at D:\dev\server\test-stack.ts:2:9
at step (D:\dev\server\test-stack.ts:31:23)
at Object.next (D:\dev\server\test-stack.ts:12:53)
at D:\dev\server\test-stack.ts:6:71
at new Promise (<anonymous>)
at __awaiter (D:\dev\server\test-stack.ts:2:12)
at thrower (D:\dev\server\test-stack.ts:37:12)
at D:\dev\server\test-stack.ts:6:16
at step (D:\dev\server\test-stack.ts:31:23)
at Object.next (D:\dev\server\test-stack.ts:12:53)
all done D:\dev\server\test-stack.ts
答案 0 :(得分:0)
经过一番研究并注意到__awaiter
之后,我决定检查什么是打字稿定位。那是我的问题。
这是我的坏tsconfig.json
:
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"downlevelIteration": true,
},
"include": [
"server/**/*", "tests"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
此"target": "es2018"
修复了该问题:
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"downlevelIteration": true,
"target": "es2018"
},
"include": [
"server/**/*", "tests"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
产生此堆栈跟踪:
ts-node test-stack.ts
node version v12.4.0
main error Error: test
at thrower (D:\dev\server\test-stack.ts:2:9)
at level1 (D:\dev\server\test-stack.ts:6:16)
at level2 (D:\dev\server\test-stack.ts:10:16)
at level3 (D:\dev\server\test-stack.ts:14:16)
at main (D:\dev\server\test-stack.ts:19:11)
at Object.<anonymous> (D:\dev\server\test-stack.ts:27:1)
at Module._compile (internal/modules/cjs/loader.js:774:30)
at Module.m._compile (C:\Users\yuv\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:439:23)
at Module._extensions..js (internal/modules/cjs/loader.js:785:10)
at Object.require.extensions.<computed> [as .ts] (C:\Users\yuv\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:442:12)
all done D:\dev\server\test-stack.ts