A small utility for safely deriving absolute file and directory paths from
import.meta
in Node.js, with a consistent fallback order.
import.meta
dirname
→ filename
→ url
)__dirname
/
__filename
Install @simbo/import-meta-path
from the npm registry:
npm i [-D] @simbo/import-meta-path
For a complete API reference, see the documentation.
import { importMetaPath } from '@simbo/import-meta-path';
// get the absolute directory path from import.meta
const dirPath = importMetaPath(import.meta);
// if importMeta has dirname → returned directly
// if only filename → dirname(filename)
// otherwise → dirname(fileURLToPath(url))
// get the absolute file path from import.meta
const filePath = importMetaPath(import.meta, 'file');
// if only filename → returned directly
// otherwise → fileURLToPath(url)