Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8779641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:50:43+00:00 2026-06-13T19:50:43+00:00

Here is some sample code from three files: // foo.js var myFunc = require(./myFunc);

  • 0

Here is some sample code from three files:

// foo.js
var myFunc = require("./myFunc");
function foo(){
   myFunc("message");
}

// bar.js
var myFunc = require("./myFunc");
function bar(){
   myFunc("message");
}

// myFunc.js
module.exports = myFunc;
function myFunc(arg1){
   console.log(arg1);
   // Here I need the file path of the caller function
   // For example, "/path/to/foo.js" and "/path/to/bar.js"
}

I need to get the file path of the caller function dynamically, without any extra argument passing, for myFunc.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:50:45+00:00Added an answer on June 13, 2026 at 7:50 pm

    You need to fiddle with the inner workings of v8. See: the wiki entry about the JavaScript Stack Trace API.

    I’ve based a little test on some code in a proposed commit and it seems to work. You end up with an absolute path.

    // omfg.js
    
    module.exports = omfg
    
    function omfg() {
      var caller = getCaller()
      console.log(caller.filename)
    }
    
    // private
    
    function getCaller() {
      var stack = getStack()
    
      // Remove superfluous function calls on stack
      stack.shift() // getCaller --> getStack
      stack.shift() // omfg --> getCaller
    
      // Return caller's caller
      return stack[1].receiver
    }
    
    function getStack() {
      // Save original Error.prepareStackTrace
      var origPrepareStackTrace = Error.prepareStackTrace
    
      // Override with function that just returns `stack`
      Error.prepareStackTrace = function (_, stack) {
        return stack
      }
    
      // Create a new `Error`, which automatically gets `stack`
      var err = new Error()
    
      // Evaluate `err.stack`, which calls our new `Error.prepareStackTrace`
      var stack = err.stack
    
      // Restore original `Error.prepareStackTrace`
      Error.prepareStackTrace = origPrepareStackTrace
    
      // Remove superfluous function call on stack
      stack.shift() // getStack --> Error
    
      return stack
    }
    

    And a test that includes omfg module:

    #!/usr/bin/env node
    // test.js
    
    var omfg = require("./omfg")
    
    omfg()
    

    And you will get on the console the absolute path of test.js.


    EXPLANATION

    This is not so much a “node.js” issue as it is a “v8” issue.

    See: Stack Trace Collection for Custom Exceptions

    Error.captureStackTrace(error, constructorOpt) adds to the error parameter a stack property, which evaluates by default to a String (by way of FormatStackTrace). If Error.prepareStackTrace(error, structuredStackTrace) is a Function, then it is called instead of FormatStackTrace.

    So, we can override Error.prepareStackTrace with our own function that will return whatever we want–in this case, just the structuredStackTrace parameter.

    Then, structuredStackTrace[1].receiver is an object representing the caller.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got some sample code from the net here: http://www.javadb.com/sending-a-post-request-with-parameters-from-a-java-class That works fine. It
I'm using simple-html-dom to try and pull some code from another site. Here's some
hope someone can answer this. Here is some sample code. namespace std { #ifdef
Here is some sample Python code: import re some_regex = re.compile(r\s+1\s+) result = some_regex.search(
I'm trying to parse a MIME-Message, using SharpMimeTools and some sample Mime Messages from
I have several dictionaries i populate from a database here is some sample of
I pasted the sample code from here in my .cshtml file just to see
Here is some simple code: DIR* pd = opendir(xxxx); struct dirent *cur; while (cur
I've got a quite strange problem here. I'm calling some simple code via Ajax.Updater:
## Sample Table ## Here are some particulars concerning proposed problem. ROWID = _id;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.