
Product
A New Overview in our Dashboard
We redesigned Socket's first logged-in page to display rich and insightful visualizations about your repositories protected against supply chain threats.
caller-path
Advanced tools
The caller-path npm package is designed to help developers retrieve the path of the file that calls a function. This can be particularly useful for debugging, logging, and tracking the flow of a program, especially in complex applications where functions are called from multiple places.
Get caller file path
This feature allows developers to log or retrieve the path of the file that called a function. In the provided code, the function `whoCalled` uses `callerPath()` to print the path of the caller file.
const callerPath = require('caller-path');
function whoCalled() {
console.log(callerPath());
}
module.exports = whoCalled;
The 'callsites' package provides functionality to get the call sites (stack frames) of the V8 stack trace API, which can be used to find the path of the caller. It offers more detailed control over the stack trace compared to caller-path, allowing developers to access deeper insights into the call stack.
This package is used to extract a stack trace from within a running node.js application, similar to caller-path. It differs in that it provides a more comprehensive set of features for manipulating and analyzing the stack trace beyond just retrieving the caller's path.
Get the path of the caller function
npm install caller-path
// foo.js
import callerPath from 'caller-path';
export default function foo() {
console.log(callerPath());
//=> '/Users/sindresorhus/dev/unicorn/bar.js'
}
// bar.js
import foo from './foo.js';
foo();
If the caller's callsite object getFileName
was not defined for some reason, it will return undefined
.
Get the path of the caller function.
Type: number
Default: 0
The caller path depth, meaning how many levels we follow back on the stack trace.
For example:
// foo.js
import callerPath from 'caller-path';
export default function foo() {
console.log(callerPath());
//=> '/Users/sindresorhus/dev/unicorn/foobar.js'
console.log(callerPath({depth: 1}));
//=> '/Users/sindresorhus/dev/unicorn/bar.js'
console.log(callerPath({depth: 2}));
//=> '/Users/sindresorhus/dev/unicorn/foo.js'
}
// bar.js
import foo from './foo.js';
export default function bar() {
foo();
}
// foobar.js
import bar from './bar.js';
bar();
FAQs
Get the path of the caller function
We found that caller-path demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
We redesigned Socket's first logged-in page to display rich and insightful visualizations about your repositories protected against supply chain threats.
Product
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
Security News
CISA denies CVE funding issues amid backlash over a new CVE foundation formed by board members, raising concerns about transparency and program governance.