In NodeJS, Given a function handle, how to find the file or module where such function was declared?
For example:
// File 1
import { test } from './file1'
function fileFile(fn: Function){
//... here's my issue
}
console.log(findFile(test)) // Logs '~/a/b/file1.js'
// File 2
export const test = ()=> true
I have tried different approaches, from using v8 to require.
I cannot find a proper way to do this.
I thought of finding the fn.toString() contents but wont be so reliable.
Thoughts?
evalinstruction. I do not think this is accessible via script viav8orvmmodules, as you tried, but you may be able to write a native addon (likely ABI unstable) that pulls the info from the function. The chain is likely: function -> script -> source url but I can't point to the exact code yet. Still looking[[FunctionLocation]]member within the Function. Will report back to see if it's works. (node 18)