util.getCallSites([frameCount][, options])


稳定性: 1.1 - 处于活跃开发中

  • frameCount <number> 可选的要捕获为调用点对象的帧数。默认值: 10。允许范围为 1 到 200。
  • options <Object> 可选
    • sourceMap <boolean> 从源映射重建堆栈跟踪中的原始位置。 默认情况下通过标志 --enable-source-maps 启用。
  • 返回值:<Object[]> 一个调用站点对象数组
    • functionName <string> 返回与此调用点关联的函数的名称。
    • scriptName <string> 返回包含此调用点对应函数脚本的资源名称。
    • scriptId <string> 返回脚本的唯一 ID,如同在 Chrome DevTools 协议 Runtime.ScriptId 中一样。
    • lineNumber <number> 返回 JavaScript 脚本的行号(从 1 开始)。
    • columnNumber <number> 返回 JavaScript 脚本的列号(从 1 开始)。

返回一个包含调用者函数堆栈的调用点对象数组。

【Returns an array of call site objects containing the stack of the caller function.】

import { getCallSites } from 'node:util';

function exampleFunction() {
  const callSites = getCallSites();

  console.log('Call Sites:');
  callSites.forEach((callSite, index) => {
    console.log(`CallSite ${index + 1}:`);
    console.log(`Function Name: ${callSite.functionName}`);
    console.log(`Script Name: ${callSite.scriptName}`);
    console.log(`Line Number: ${callSite.lineNumber}`);
    console.log(`Column Number: ${callSite.column}`);
  });
  // CallSite 1:
  // Function Name: exampleFunction
  // Script Name: /home/example.js
  // Line Number: 5
  // Column Number: 26

  // CallSite 2:
  // Function Name: anotherFunction
  // Script Name: /home/example.js
  // Line Number: 22
  // Column Number: 3

  // ...
}

// A function to simulate another stack layer
function anotherFunction() {
  exampleFunction();
}

anotherFunction();const { getCallSites } = require('node:util');

function exampleFunction() {
  const callSites = getCallSites();

  console.log('Call Sites:');
  callSites.forEach((callSite, index) => {
    console.log(`CallSite ${index + 1}:`);
    console.log(`Function Name: ${callSite.functionName}`);
    console.log(`Script Name: ${callSite.scriptName}`);
    console.log(`Line Number: ${callSite.lineNumber}`);
    console.log(`Column Number: ${callSite.column}`);
  });
  // CallSite 1:
  // Function Name: exampleFunction
  // Script Name: /home/example.js
  // Line Number: 5
  // Column Number: 26

  // CallSite 2:
  // Function Name: anotherFunction
  // Script Name: /home/example.js
  // Line Number: 22
  // Column Number: 3

  // ...
}

// A function to simulate another stack layer
function anotherFunction() {
  exampleFunction();
}

anotherFunction();

通过将选项 sourceMap 设置为 true,可以重建原始位置。 如果源映射不可用,原始位置将与当前位置相同。 当启用 --enable-source-maps 标志时,例如使用 --experimental-transform-typessourceMap 将默认为 true。

【It is possible to reconstruct the original locations by setting the option sourceMap to true. If the source map is not available, the original location will be the same as the current location. When the --enable-source-maps flag is enabled, for example when using --experimental-transform-types, sourceMap will be true by default.】

import { getCallSites } from 'node:util';

interface Foo {
  foo: string;
}

const callSites = getCallSites({ sourceMap: true });

// With sourceMap:
// Function Name: ''
// Script Name: example.js
// Line Number: 7
// Column Number: 26

// Without sourceMap:
// Function Name: ''
// Script Name: example.js
// Line Number: 2
// Column Number: 26 
const { getCallSites } = require('node:util');

const callSites = getCallSites({ sourceMap: true });

// With sourceMap:
// Function Name: ''
// Script Name: example.js
// Line Number: 7
// Column Number: 26

// Without sourceMap:
// Function Name: ''
// Script Name: example.js
// Line Number: 2
// Column Number: 26