Skip to content

Instantly share code, notes, and snippets.

@capaj
Last active March 11, 2025 17:23
Show Gist options
  • Save capaj/a9ba9d313b79f1dcd9a2 to your computer and use it in GitHub Desktop.
Save capaj/a9ba9d313b79f1dcd9a2 to your computer and use it in GitHub Desktop.
showcase of module.parent
var myFunc = require("./myFunc");
(function bar(){
myFunc("bar message");
})();
var myFunc = require("./myFunc");
(function foo(){
myFunc("foo message");
})();
function myFunc(arg){
console.log(arg);
console.log(module.parent.filename);
// Here I need the file path of the caller function
// For example, "/path/to/foo.js" and "/path/to/bar.js"
}
module.exports = myFunc;
@pevey
Copy link

pevey commented Mar 11, 2025

module.parent is now deprecated, but this approach still works using require.main.filename. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment