locateV8() returns the absolute source file path for a function or class at runtime.
npm install function-locationSupports Node.js 20 through 26 on Linux x64 (glibc), Windows x64, and macOS (x64 and arm64).
import { locateV8 } from 'function-location';
class ExampleClass {}
function exampleFunction() {}
locateV8(ExampleClass); // /path/to/file.ts
locateV8(exampleFunction); // /path/to/file.tslocateV8(input: Function): string | undefined
- Works for classes, functions, methods, arrow functions, and async/generator functions, named or anonymous.
- Bound functions resolve to the location of their target function.
- Functions compiled with
vmresolve to the scriptfilename. - Returns
undefinedwhen no source file exists: builtins (Math.max,Array), functions created byevalornew Function, and proxies. - Throws
TypeError: Function argument expectedwheninputis not a function.
This library uses a synchronous native addon lookup instead of the inspector protocol.
Sample comparison against an inspector-protocol baseline:
| Approach | Median time / call | Relative speed |
|---|---|---|
locateV8 |
0.0254 µs |
14438.93x faster |
inspector protocol |
367.4543 µs |
baseline |
Results vary by environment; treat them as sample measurements only. Methodology and raw samples: docs/BENCHMARK.md
