What is caller-path?
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.
What are caller-path's main functionalities?
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;
Other packages similar to caller-path
callsites
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.
stack-trace
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.
caller-path
Get the path of the caller module
You can't use module.parent
as modules are cached and it will return the first caller module, not necessarily the current one.
Install
$ npm install --save caller-path
Usage
var callerPath = require('caller-path');
module.exports = function () {
console.log(callerPath());
}
var foo = require('./foo');
foo();
License
MIT © Sindre Sorhus