vanipath
v1.0.8
Published
Get __dirname and __filename in ESM & CJS using pure JavaScript
Readme
vanipath
Drop-in replacement for
__filenameand__dirnamein both ESM and CommonJS – pure JavaScript, no native bindings.
vanipath provides functions that act as direct replacements for Node.js's __filename and __dirname, working seamlessly in both ESM and CJS environments. It's designed for developers who need compatibility across different module systems.
No native modules, and no build steps required. Just import and use like the originals.
Features
- ✅ Zero dependencies — no bloat, no surprises
- ✅ No native modules, and no build steps required
- ✅ Especially useful in projects using cross-environment build tools like tsup, vite, or esbuild, where
__dirnameand__filenamemight not behave consistently or may require workarounds
🌐 Compatibility
| Runtime | Support | Notes | |-----------|--------- |-----------------------------------------| | Node.js | ✅ | Full support (ESM & CJS) | | Deno | ✅ | Full support (ESM & CJS) | | Bun | ✅ | Full support (ESM & CJS) |
🚀 Installation
#nodejs
npm install vanipath
pnpm add vanipath
#bun
bun add vanipath
#deno
deno add npm:vanipath🛠 Usage
In ESM:
import { filename, dirname } from 'vanipath';
console.log(filename()); // equivalent to __filename
console.log(dirname()); // equivalent to __dirnameIn CommonJS:
const { filename, dirname } = require('vanipath');
console.log(filename()); // equivalent to __filename
console.log(dirname()); // equivalent to __dirnameWorks exactly like native __filename and __dirname, even in environments where they’re not available (like ESM modules).
🧪 Test
This project uses Vitest:
npm test
# or
npx vitest run📦 Changelog
See full release notes in CHANGELOG.md
📄 License
MIT License © 2025 yukiakai
