Wongzigii Wongzigii - 1 month ago 7x
Node.js Question

How do you find out the caller function which is in different file?


module.exports.foo = function foo() {

// Bar.js

var f = require(./foo);

function bar() {
f.foo(); // How to find out the caller function is `foo` ?

How do you find out the caller function which is in different file in JavaScript?

I've seen a similar answer in this question.

But I got this logging :

debug: caller is function wrapper() {
var fn = (this && this !== root && this instanceof wrapper) ? Ctor : func;
return fn.apply(thisArg, arguments);


I am writing a global function to log the error from the module it is calling, so here I am trying to find out the caller function.

If you do really need to do that, I'd probably do it via Error's stack:

function log() {
    var stack = new Error().stack;
    // ...log the stack

If you like, you can do some post-processing on the stack string, but I would tend to avoid it as the exact format can evolve from dot release to dot release of V8 (I can tell from your code you're using NodeJS).

Example (requires a browser that supports Error#stack, like Chrome [which uses the same JavaScript engine as NodeJS] or Firefox):

function log() {
  console.log(new Error().stack);

function foo() {

function bar() {