Skip to the solution
EasyPhone Screen6 min read

What is the purpose of __dirname and __filename in Node.js?

304 views

The problem

Question presented to candidate: "Your code does fs.readFileSync('./config.json') and it works when you run it from the project root, but breaks when a teammate runs it from a different directory. What went wrong, and what should you have used instead?"

What a strong answer should cover:

  • __dirname and __filename are two of the five parameters supplied by Node's implicit CommonJS module wrapper β€” the current module file's absolute directory path and absolute file path, respectively.
  • πŸ“Œ The bug they fix: a relative path like ./config.json is resolved against the process's current working directory (wherever the node command was actually invoked from), not against the file's own location. path.join(__dirname, "config.json") is resolved against the file's own location, which is what most code actually means when it says "the config file next to me."
  • __dirname/__filename exist only in CommonJS files β€” in an ES Module, there is no such implicit variable; the equivalent is derived from import.meta.url (typically via fileURLToPath(import.meta.url) and path.dirname(...) of that).
  • Running code with node -e "..." or via the REPL produces misleading placeholder values for these (. and [eval]) rather than a real path β€” a good answer notes this rather than testing/demonstrating the concept that way and drawing the wrong conclusion from it.
  • process.cwd() is the commonly confused alternative β€” it returns the current working directory of the process, which changes based on how and from where the script was launched, and is a fundamentally different, session-dependent value from a file's own fixed location.
  • A precise answer names the practical rule: any path meant to be relative to the source file itself (a config file shipped next to the code, a template, a static asset) should be built with __dirname/import.meta.url, never a bare relative string β€” those are the correct tool only for a path meant to be relative to wherever the process happens to be invoked from.

Clarifying questions expected:

  • "Is this CommonJS or an ES Module?" β€” the mechanism (implicit variable vs. import.meta.url) differs.
  • "Does the path actually need to be relative to the invoking directory, or to the source file?" β€” this is the real distinction the bug in the prompt is about.

Code / implementation expected: Yes β€” showing the actual absolute values from a real file, and the ESM equivalent via import.meta.url, is the concrete deliverable.

  • global variables
  • paths
  • modules
  • commonjs

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 8 sections

Read the code

JavaScriptReal __dirname/__filename values from an actual file, the bug they fix, and the ESM equivalent
// dirtest.cjs β€” run as: node dirtest.cjs
console.log("__dirname:", __dirname);
console.log("__filename:", __filename);
// __dirname: C:\...\node-batch5
// __filename: C:\...\node-batch5\dirtest.cjs

// BROKEN β€” depends on wherever "node" was invoked from:
const data = fs.readFileSync("./config.json");
// CORRECT β€” anchored to this file's own fixed location:
const data2 = fs.readFileSync(path.join(__dirname, "config.json"));

// The ES Modules equivalent (no __dirname/__filename there):
import { fileURLToPath } from "node:url";
import path from "node:path";
const __filenameESM = fileURLToPath(import.meta.url);
const __dirnameESM = path.dirname(__filenameESM);

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 28 of 152 in Node.js

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Node.js questions
Up nextEasy

Explain the purpose of `package.json` and `npm`.

Continue