@simbo/import-meta-path
v1.0.0
Published
A small utility for safely deriving absolute file and directory paths from import.meta in Node.js, with a consistent fallback order.
Maintainers
Readme
Import Meta Path
A small utility for safely deriving absolute file and directory paths from
import.meta in Node.js, with a consistent fallback order.
Features
- Derive absolute directory or file path from
import.meta - Consistent fallback order (
dirname→filename→url) - Node.js ESM-only, works as a drop-in replacement for
__dirname/__filename - Zero dependencies
Installation
Install @simbo/import-meta-path from the npm registry:
npm i [-D] @simbo/import-meta-pathUsage
For a complete API reference, see the documentation.
Example
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)