ENOENT
Linux / POSIXERRORCommonFile SystemHIGH confidence

No Such File or Directory

What this means

The specified path does not exist. Either the file or directory at that path has never been created, has been deleted, or a parent directory in the path is missing. This is one of the most common errors in system programming.

Why it happens
  1. 1The path contains a typo or incorrect capitalization on a case-sensitive filesystem.
  2. 2The file was deleted by another process between the existence check and the open call.
  3. 3A parent directory in the path does not exist.
  4. 4A symbolic link in the path points to a non-existent target.
How to reproduce

Opening a file that does not exist on disk.

trigger — this will error
trigger — this will error
$ cat /tmp/does-not-exist.txt
cat: /tmp/does-not-exist.txt: No such file or directory

expected output

cat: /tmp/does-not-exist.txt: No such file or directory
$ echo $?
1

Fix 1

Verify the path exists before opening

WHEN When reading optional configuration files

Verify the path exists before opening
const fs = require("fs");
if (fs.existsSync("/tmp/config.json")) {
  const data = fs.readFileSync("/tmp/config.json", "utf8");
}

Why this works

Checking existence before open avoids the error for optional files.

Fix 2

Create parent directories before writing

WHEN When creating new files in potentially missing directories

Create parent directories before writing
const fs = require("fs");
fs.mkdirSync("/tmp/myapp/logs", { recursive: true });
fs.writeFileSync("/tmp/myapp/logs/app.log", "");

Why this works

The recursive option creates all missing parent directories in a single call.

Sources
Official documentation ↗

Linux Programmer Manual errno(3)

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Linux / POSIX errors