No Such File or Directory
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.
- 1The path contains a typo or incorrect capitalization on a case-sensitive filesystem.
- 2The file was deleted by another process between the existence check and the open call.
- 3A parent directory in the path does not exist.
- 4A symbolic link in the path points to a non-existent target.
Opening a file that does not exist on disk.
$ 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
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
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.
Linux Programmer Manual errno(3)
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev