How to use the get function from stack-trace

Find comprehensive JavaScript stack-trace.get code examples handpicked from public code repositorys.

stack-trace.get is a function that captures the call stack at the point where it is called and returns an array of stack frames.

99
100
101
102
103
104
105
106
107
108
if(!this.rootPath){
    console.log('ATENCIÓN hay que poner be.rootPath antes de llamar a super()');
    this.rootPath=Path.resolve(__dirname,'..');
}
console.log('rootPath',this.rootPath);
var trace = stackTrace.get();
trace.forEach(function(callSite){
    var path = callSite.getFileName();
    if(path && !path.startsWith('internal/') && !path.startsWith('node:internal/')){
        path = Path.dirname(path);
fork icon3
star icon3
watch icon10

+ 22 other calls in file

34
35
36
37
38
39
40
41
42
43
44
function getFunctionCaller(func) {
    return func.caller;
}


function getFunctionData(func) {
    const trace = stackTrace.get(func || getFunctionCaller(getFunctionData));
    const callerData = trace[0];
    const data = {
        filePath: `${callerData.getFileName()}:${callerData.getLineNumber()}:${callerData.getColumnNumber()}`,
    };
fork icon0
star icon3
watch icon211

+ 14 other calls in file

How does stack-trace.get work?

stack-trace.get is a function in Node.js that returns an array of CallSite objects representing the current stack trace. It can be used to introspect the current execution context of a program and diagnose errors or performance issues. The returned CallSite objects contain information about the caller and callee functions, including their names, file locations, and argument values.

57
58
59
60
61
62
63
64
65
66
67
}


var processMeta = function (meta) {
    var callers
    try {
        callers = stackTrace.get().slice(2)
            .filter(c => c.getFileName())
            .filter(c => c.getFileName().indexOf('/app/') > -1)
            .map(caller => `${(caller.getFunctionName() || caller.getMethodName())}@${caller.getFileName()}:${caller.getLineNumber()}`)
            .join('\n')
fork icon0
star icon0
watch icon2

+ 22 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const stackTrace = require("stack-trace");

function foo() {
  bar();
}

function bar() {
  baz();
}

function baz() {
  console.log(stackTrace.get());
}

foo();

In this example, stack-trace.get is used to retrieve a stack trace and print it to the console. The get function returns an array of stack frames, where each frame contains information about a function call in the stack. By calling stackTrace.get() inside the baz function and logging the result to the console, we can see the current call stack and the functions that led up to the current point of execution.