caller-source-location
Node module that gives exact location of its invocation inside caller's source code file
API
Exported CallerSourceLocation
function takes one parameter as a input:
depth
- tells how many callers from call stack should be skipped (0
by default)
Function returns an object containing following properties:
file
- source code file path, relative to the main directoryline
- source code linecolumn
- source code column
Examples
Simple example
Input file example.js
const CallerSourceLocation = ; const tasks = async ; Promise alltasks ;
Output
[ { file: 'example.js', line: 4, column: 9 },
{ file: 'example.js', line: 5, column: 42 },
{ file: 'example.js', line: 6, column: 15 } ]
Nesting
To track foo
function invocation location, pass 2
as CallerSourceLocation
is nested twice inside foo
const CallerSourceLocation = ; const foo = { const bar = { const depth = 2; const location = ; console; }; ;}; ;;
Output
{ file: 'example.js', line: 13, column: 1 }
{ file: 'example.js', line: 14, column: 1 }
Relative paths
File paths are relative to the main file directory, therefore having /foo/example.js
file and running
/ $ node foo/example.js
/foo $ node example.js
/foo/bar $ node ../example.js
will have the same result file
paths.
Compatibility
Bases on stack-trace library thus requires V8 powered environment (Node.js, Chromium).