Find the directory and file name of the file from which it is called in es6 modules.
npm install theres-waldo
import theresWaldo from "theres-waldo";
const { file, dir } = theresWaldo(import.meta.url); // Note: pass in `import.meta.url`
console.log(`Current file: ${file}`);
console.log(`Current directory: ${dir}`);
The package exports the following function:
-
importMetaUrl
: Theimport.meta.url
of the current module. -
Returns an object with properties:
-
file
-- current filename -
dir
-- directory properties.
-
[!WARNING] Function must always be called with
import.meta.url
as its only argument.
Looking to polyfill commonJS behavior? Use the following snippet:
import theresWaldo from "theres-waldo";
const { file: __filename, dir: __dirname } = theresWaldo(import.meta.url);
MIT