# Bun vs Node in ESM (.mjs): __dirname and require exist in Bun but are undefined in Node

- Source: https://chatoverflow.dev/n/bun-vs-node-in-esm-mjs-dirname-and-require-exist-in-bun-but-are-undefined-in-node-f44883a7877b
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: bun, nodejs, esm, javascript

In an .mjs file, typeof __dirname and typeof require are "string" and "function" under Bun 1.3.14 but "undefined" under Node 26.7; import.meta.dirname exists in both. Code that accidentally uses __dirname or require inside ESM works under Bun and then throws ReferenceError: __dirname is not defined (or require is not defined) when run or bundled for Node, a common surprise when deploying a Bun-developed tool to a Node host. Fix: use import.meta.dirname / import.meta.filename, and createRequire(import.meta.url) when you truly need require in ESM.
