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


稳定性: 1.1 - 积极开发

¥Stability: 1.1 - Active development

  • frameCount <number> 要捕获为调用站点对象的可选帧数。默认值:10。允许的范围在 1 到 200 之间。

    ¥frameCount <number> Optional number of frames to capture as call site objects. Default: 10. Allowable range is between 1 and 200.

  • options <Object> 可选的

    ¥options <Object> Optional

    • sourceMap <boolean> 从源映射重建堆栈跟踪中的原始位置。默认情况下,使用标志 --enable-source-maps 启用。

      ¥sourceMap <boolean> Reconstruct the original location in the stacktrace from the source-map. Enabled by default with the flag --enable-source-maps.

  • 返回:<Object[]> 调用站点对象数组

    ¥Returns: <Object[]> An array of call site objects

    • functionName <string> 返回与此调用站点关联的函数的名称。

      ¥functionName <string> Returns the name of the function associated with this call site.

    • scriptName <string> 返回包含此调用站点的函数脚本的资源的名称。

      ¥scriptName <string> Returns the name of the resource that contains the script for the function for this call site.

    • scriptId <string> 返回脚本的唯一 ID,如 Chrome DevTools 协议 Runtime.ScriptId 中所述。

      ¥scriptId <string> Returns the unique id of the script, as in Chrome DevTools protocol Runtime.ScriptId.

    • lineNumber <number> 返回 JavaScript 脚本行号(从 1 开始)。

      ¥lineNumber <number> Returns the JavaScript script line number (1-based).

    • columnNumber <number> 返回 JavaScript 脚本列号(从 1 开始)。

      ¥columnNumber <number> Returns the JavaScript script column number (1-based).

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

¥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-types 时,sourceMap 默认为 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