1. caller-path
Get the path of the caller function
caller-path
Package: caller-path
Created by: sindresorhus
Last modified: Mon, 10 Jul 2023 23:17:06 GMT
Version: 4.0.0
License: MIT
Downloads: 27,793,693
Repository: https://github.com/sindresorhus/caller-path

Install

npm install caller-path
yarn add caller-path

caller-path

Get the path of the caller function

Install

 npm install caller-path

Usage

 // 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.

API

callerPath(options?)

Get the path of the caller function.

depth

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();

Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code