简体   繁体   English

Node.js stacktraces不包含用户代码

[英]Node.js stacktraces don't include user code

When debugging Node.js code I often encounter call stacks that do not include my program's code in them, only node_modules/non-user code, despite the current line of execution being at a location in my code. 在调试Node.js代码时,我经常会遇到不包含程序代码的调用堆栈,只有node_modules /非用户代码,尽管当前的执行行是在我的代码中的某个位置。 This defeats the purpose of following the call stack to see the execution path through my application code. 这违背了跟随调用堆栈通过我的应用程序代码查看执行路径的目的。

Why are my source files not showing in the call stack? 为什么我的源文件没有显示在调用堆栈中?

完整的堆栈跟踪

It appears that you're looking at an asynchronous stack trace where your code is not in the stack except for your callback because your code unwound/finished and THEN the async callback was called. 您似乎正在查看异步堆栈跟踪,其中您的代码不在堆栈中,除了回调,因为您的代码已解除/完成,然后调用了异步回调。

All .then() handlers for all promises are called asynchronously with a clean stack. 所有promises的所有.then()处理程序都与一个干净的堆栈异步调用。 That's per the promise specification. 这是承诺规范。 So, promises always let the current thread of execution finish and unwind and then they fire .then() handlers with no user code on the stack when the callback is called. 所以,promises总是让当前的执行线程完成并解除,然后在调用回调时它们会.then()处理程序,而堆栈上没有用户代码。 What you are describing is how synchronous code would work, not asynchronous code. 您所描述的是同步代码如何工作,而不是异步代码。 We could talk a lot more specifically instead of theoretically if you showed actual code and described where you're looking at the call stack. 如果您展示实际代码并描述您正在查看调用堆栈的位置,我们可以更具体地讨论,而不是理论上。

Async progress often has to be tracked with logging because you can't easily step through it and you can't just break and look at stack traces either. 通常必须使用日志记录跟踪异步进度,因为您无法轻松地逐步执行它,您也不能只是打破并查看堆栈跟踪。

As a little simpler example to look at: 作为一个更简单的例子来看:

function foo() {
   setTimeout(() => {
       console.log("timer");    // set breakpoint here
   }, 100);
}

foo();

The function foo() has finished executing and returned before the callback is called and thus the stack trace will not have any of your code (other than just the callback) on it. 函数foo()已经完成执行并在调用回调之前返回,因此堆栈跟踪将不会包含任何代码(除了回调之外)。

While .then() handlers use a slightly different scheduler than setTimeout() , the principle is the same. 虽然.then()处理程序使用的调度程序与setTimeout()略有不同,但原理是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM