JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 42
  • Score
    100M100P100Q52224F
  • License MIT

A utility for stack tracing based on the NodeJS.CallSite object, enabling dynamic inspection of function calls.

Package Exports

  • @mnrendra/stack-trace
  • @mnrendra/stack-trace/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@mnrendra/stack-trace) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@mnrendra/stack-trace

npm version types license

A utility for stack tracing based on the NodeJS.CallSite object, enabling dynamic inspection of function calls.
Useful for debugging, logging, or building tools that need to understand call origins and file references at runtime.

Install

npm i @mnrendra/stack-trace

Usage

  • traceStacks(targetFn?, options?):
    Returns an array of NodeJS.CallSite objects representing the captured stack trace.
  • traceFiles(targetFn?, options?):
    Returns an array of file names (as string in CommonJS or URL in ES Modules) extracted from the stack trace of the specified function.
  • traceFnNames(targetFn?, options?):
    Returns an array of function names extracted from the stack trace of the given function.

Note: If targetFn is not provided, it captures the current call stack.

Usage in CommonJS:

const { traceStacks, traceFiles, traceFnNames } = require('@mnrendra/stack-trace')

const caller1 = () => traceStacks()
const [stack] = caller1()
console.log(stack.getFileName() === __filename) // Output: true

const caller2 = () => traceFiles()
const [file] = caller2()
console.log(file === __filename) // Output: true

const caller3 = () => traceFnNames()
const [fnName] = caller3()
console.log(fnName) // Output: caller3

Usage in ES Modules:

import { traceStacks, traceFiles, traceFnNames } from '@mnrendra/stack-trace'

const caller1 = () => traceStacks()
const [stack] = caller1()
console.log(stack.getFileName() === import.meta.url) // Output: true

const caller2 = () => traceFiles()
const [file] = caller2()
console.log(file === import.meta.url) // Output: true

const caller3 = () => traceFnNames()
const [fnName] = caller3()
console.log(fnName) // Output: caller3

Examples

  1. Call from your development project /foo/project-name/src/index.mjs:
import { traceStacks, traceFiles, traceFnNames } from '@mnrendra/stack-trace'

const caller1 = () => traceStacks()
const [stack] = caller1()
console.log(stack.getFileName()) // Output: file:///foo/project-name/src/index.mjs

const caller2 = () => traceFiles()
const [file] = caller2()
console.log(file) // Output: file:///foo/project-name/src/index.mjs

const caller3 = () => traceFnNames()
const [fnName] = caller3()
console.log(fnName) // Output: caller3
  1. Call from your production module /foo/project-name/node_modules/module-name/dist/index.js:
"use strict";

const { traceStacks, traceFiles, traceFnNames } = require('@mnrendra/stack-trace');

const caller1 = () => traceStacks();
const [stack] = caller1();
console.log(stack.getFileName()); // Output: /foo/project-name/node_modules/module-name/dist/index.js

const caller2 = () => traceFiles();
const [file] = caller2();
console.log(file); // Output: /foo/project-name/node_modules/module-name/dist/index.js

const caller3 = () => traceFnNames();
const [fnName] = caller3();
console.log(fnName); // Output: caller3

Note: When calling getFileName in an ES Modules module, the file name will be returned as a URL instead of a file path.

Options

import { traceStacks, traceFiles, traceFnNames } from '@mnrendra/stack-trace'

const options = {
  limit: 10 // The maximum number of stack frames to capture (default: 10).
}

const caller1 = () => traceStacks(
  caller1, // The option target function to be traced.
  options
)

const caller2 = () => traceFiles(
  caller2, // The option target function to be traced.
  options
)

const caller3 = () => traceFnNames(
  caller3, // The option target function to be traced.
  options
)

Types

import type {
  CallSite, // The alias of `NodeJS.CallSite`.
  Options, // The options object for `traceStacks`, `traceFiles`, or `traceFnNames`.
} from '@mnrendra/stack-trace'

License

MIT

Author

@mnrendra