Skip to main content

Node.js · 5 common causes

ENOENT: no such file or directory

Updated

Node asked the operating system for a path that does not exist — usually resolved from a different working directory than you expected.

The exact message

node:internal/fs/utils:347
    throw err;
    ^

Error: ENOENT: no such file or directory, open 'config.json'
    at Object.openSync (node:fs:603:3)
    at Object.readFileSync (node:fs:471:35) {
  errno: -2,
  syscall: 'open',
  code: 'ENOENT',
  path: 'config.json'
}

Paths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is ENOENT: no such file or directory.

How to fix it

Cause 1 of 5

The path is relative, and the process started from a different directory.

A bare relative path resolves against process.cwd(), not against the file doing the reading. Anchor it to the module instead: path.join(import.meta.dirname, 'config.json') in ESM, or path.join(__dirname, 'config.json') in CommonJS.

Cause 2 of 5

The file genuinely is not there.

Log the resolved absolute path before opening it, then check that exact path with ls. In a container, the usual reason is a .dockerignore entry quietly dropping the file from the build context.

Cause 3 of 5

A parent directory does not exist on a write.

fs.writeFile creates files, never folders. Create the directory first: await fs.mkdir(path.dirname(target), { recursive: true }), which is a no-op when it already exists.

Cause 4 of 5

The name differs in case, or carries a hidden extension.

Linux is case-sensitive where your laptop is not. Windows also hides known extensions, so config.json on screen can be config.json.txt on disk — ls -la or dir /a shows the real name.

Cause 5 of 5

The ENOENT is about a command, not a data file.

spawn('somecli') throws ENOENT when the binary is not on PATH — the missing 'file' is the executable. Check with which somecli, and on Windows remember that .cmd shims need shell: true to be found.

Stopping it coming back

Build file paths from the module location rather than the working directory, and let the process fail loudly at startup if a required file is missing instead of at the first request.

ENOENT: no such file or directory — common questions

Why does it work with npm start but not when I run node directly?
npm scripts run with the package directory as the working directory; running node from somewhere else does not. That single difference explains most ENOENTs that appear only in one of the two.
How do I check whether a file exists before reading it?
Prefer just reading it and catching the ENOENT. A separate fs.existsSync check introduces a race — the file can disappear between the check and the read — and doubles the syscalls for no benefit.
The path in the error looks right. What now?
Print it with delimiters around it, for example console.log(JSON.stringify(target)). A trailing space or newline picked up from an environment variable is invisible in normal output and is a frequent cause.

Test the fix before you paste it back

Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.

Open the editor