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.
- ✅ 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
| Runtime | Support | Notes |
|---|---|---|
| Node.js | ✅ | Full support (ESM & CJS) |
| Deno | ✅ | Full support (ESM & CJS) |
| Bun | ✅ | Full support (ESM & CJS) |
#nodejs
npm install vanipath
pnpm add vanipath
#bun
bun add vanipath
#deno
deno add npm:vanipathIn 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).
This project uses Vitest:
npm test
# or
npx vitest runSee full release notes in CHANGELOG.md
MIT License © 2025 yukiakai