Skip to content

Repository files navigation

vanipath

NPM Version NPM Downloads

Build Status codecov

Drop-in replacement for __filename and __dirname in 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 __dirname and __filename might 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 __dirname

In CommonJS:

const { filename, dirname } = require('vanipath');

console.log(filename()); // equivalent to __filename
console.log(dirname());  // equivalent to __dirname

Works 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


About

Get __dirname and __filename in ESM & CJS using pure JavaScript — zero dependencies.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages