Lingua-e
JavaScript

What is a ENOENT: no such file or directory?

← Errors

ENOENT is a POSIX error code meaning 'Error NO ENTry'. In Node.js it is thrown whenever a file system operation (read, write, delete, rename) targets a path that does not exist. It is the Node.js equivalent of Python's FileNotFoundError.

Why it happens

  • The file path is relative and the process's current working directory is not what the developer expected.
  • The file was never created, was deleted, or was placed in a different directory.
  • A typo in the file name or path, including wrong case on case-sensitive file systems.
  • A build step that should generate the file failed silently, leaving the path empty.

Examples

JavaScript

Code that triggers the error

const fs = require("fs");

const content = fs.readFileSync("./config.json", "utf8");
console.log(content);

Error output

Error: ENOENT: no such file or directory, open './config.json'
    at Object.openSync (node:fs:601:18)
    at Object.readFileSync (node:fs:469:35)

How to fix it

Use path.join(__dirname, 'filename') to build paths relative to the current module rather than the process working directory. Check that the file exists with fs.existsSync() before reading it. When the missing file is generated by a build tool, ensure the build step ran successfully before the Node.js process starts.

Fixed code

const fs = require("fs");
const path = require("path");

const configPath = path.join(__dirname, "config.json");

if (!fs.existsSync(configPath)) {
  console.error("config.json not found at", configPath);
  process.exit(1);
}

const content = fs.readFileSync(configPath, "utf8");
console.log(content);

Related errors

Ready to practice your English at work?

Lingua-e has interactive exercises built around real developer conversations: standups, code reviews, retrospectives, and more. Practice until it comes naturally.

Try Lingua-e for free