Last active
March 11, 2025 17:23
-
-
Save capaj/a9ba9d313b79f1dcd9a2 to your computer and use it in GitHub Desktop.
showcase of module.parent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var myFunc = require("./myFunc"); | |
(function bar(){ | |
myFunc("bar message"); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var myFunc = require("./myFunc"); | |
(function foo(){ | |
myFunc("foo message"); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
module.parent is now deprecated, but this approach still works using require.main.filename. Thank you!