ENOENT and Relative Paths
A relative path is resolved against the calling process's current working directory, so it works in the shell but fails in a program that a service manager, IDE, or cron started somewhere else. Anchor the path to the script's own directory or another absolute location instead of relying on the launcher's directory.
The short answer
ENOENT is errno 2, "No such file or directory", raised when the kernel cannot resolve a pathname component. A relative path resolves from the calling process's current working directory, not from the directory where you typed the command, and not from the directory that holds the script. A shell runs cat ./config.json with the working directory you see at the prompt, so it succeeds. A program started by systemd, cron, an IDE run configuration, a container entrypoint, or a test runner often starts somewhere else, so ./config.json points at a different location and the open fails.
A minimal example
Create the same file and read it two ways.
mkdir -p /tmp/path-check/app && cd /tmp/path-check/app
printf '{"port": 8080}\n' > config.json
cat ./config.json
The shell succeeds because its working directory is /tmp/path-check/app. The Node.js version fails when the program is launched from /tmp/path-check:
// app.js
const fs = require("node:fs");
const text = fs.readFileSync("./config.json", "utf8");
Error: ENOENT: no such file or directory, open './config.json'
at Object.openSync (node:fs:596:3) {
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: './config.json'
}
The node:fs line and column in that stack vary by Node.js version; the stable fields are errno, code, syscall, and path.
Python behaves the same way:
from pathlib import Path
text = Path("config.json").read_text(encoding="utf-8")
# FileNotFoundError: [Errno 2] No such file or directory: 'config.json'
Changing the launch directory can hide the problem, but the robust fix is to anchor the path to a location that does not depend on the launcher. In CommonJS, __dirname is the directory of the current module. In Python, __file__ is the path of the current source file.
const path = require("node:path");
const file = path.join(__dirname, "config.json");
from pathlib import Path
file = Path(__file__).resolve().parent / "config.json"
Why this happens
Each process owns a working directory. It is inherited across fork and exec and changed only by chdir(2) or fchdir(2). When a process calls open("config.json"), the kernel begins resolution at that directory. The shell does not rewrite relative arguments into absolute ones before executing a command, so the string reaches the program unchanged. If the program's inherited directory is not the one you expected, resolution starts in the wrong place. This is the mechanism described in process working directory.
Error objects repeat the path exactly as passed, so Node's errno, code, syscall, and path fields show ./config.json even though the kernel tried the working directory plus that name. Printing process.cwd() or os.getcwd() confirms the mismatch in one line.
Practical checks
| Check | Command |
|---|---|
| Working directory | node -e "console.log(process.cwd())" |
| Working directory, Python | python -c "import os; print(os.getcwd())" |
| Absolute target | python -c "import os; print(os.path.abspath('config.json'))" |
| Every path component | namei -l ./config.json |
| Syscalls during startup | strace -f -e trace=openat,newfstatat,chdir ./app |
For a path that must exist wherever the process starts, resolve it once at startup and log the resolved absolute path. In Python, Path.resolve() also normalises symlinks and .., which matters when launchers use different spellings of the same directory; see resolving paths with pathlib.
Edge cases
ENOENT does not always mean the final file is missing. Any component can be absent, including a parent directory that the program itself was supposed to create. A missing interpreter in a #! line also produces ENOENT for a script that exists, because the kernel reports the failed interpreter lookup. When the path exists but a component is a regular file rather than a directory, the kernel returns ENOTDIR instead, which is a useful signal that the path is wrong somewhere earlier than the leaf. Finally, on a case-sensitive filesystem a name that differs only in letter case returns ENOENT even though an editor or remote listing may display it, so compare the exact bytes of the name before changing code.
Sources
Revision history
- Published
- Updated
- — Initial publication.