How to Fix Node.js Error: ENOENT: No Such File or Directory
Error: ENOENT: no such file or directory means Node.js tried to open a path that does not exist. When the app writes a file, the usual cause is a missing parent directory - Node.js does not create folders for you. Read the path from the error, then create the directory with mkdir -p or in your startup code.
What the error looks like
node:fs:1234
handleErrorFromBinding(ctx);
^
Error: ENOENT: no such file or directory, open '/srv/notify/logs/app.log'
at Object.openSync (node:fs:...)
at Object.appendFileSync (node:fs:...)
at log (/srv/notify/app.js:12:8)
{
errno: -2,
syscall: 'open',
code: 'ENOENT',
path: '/srv/notify/logs/app.log'
}
The useful fields:
code: 'ENOENT'- "error, no entry". The path does not exist.syscall- what Node.js tried:open,stat,scandir,mkdir.path- the exact path. Copy it.
Writing a file vs reading a file
The fix depends on what the app was doing:
- Reading a file (
readFileSync,require,stat): the file itself is missing or the path is wrong. - Writing or appending (
writeFileSync,appendFileSync,createWriteStream): Node.js creates the file if needed, but not its parent folder. Iflogs/does not exist, the write fails with ENOENT.
The stack trace above shows appendFileSync, so the missing piece is the
folder /srv/notify/logs/.
Step 1: check what exists
ls -la /srv/notify/
If there is no logs/ entry, you have found the cause. This often happens
after a fresh server setup or a new container image: the code is the same, but
a folder that someone created by hand long ago is gone.
Step 2: create the directory
mkdir -p /srv/notify/logs
-p creates every missing parent and does nothing if the folder already
exists, so it is safe to run more than once. Make sure the user that runs the
app can write there (chown it if the app does not run as root).
Restart the app and confirm:
curl -s http://localhost:3000/health
tail -n 3 /srv/notify/logs/app.log
Step 3: make the app create its own folders
A better long-term change is to create the directory at startup, before the first write:
const fs = require('fs');
const path = require('path');
const LOG_DIR = path.join(__dirname, 'logs');
fs.mkdirSync(LOG_DIR, { recursive: true }); // no error if it exists
const LOG_FILE = path.join(LOG_DIR, 'app.log');
recursive: true behaves like mkdir -p. Now the app works on any new
machine.
Other common ENOENT causes
- Relative paths -
fs.readFileSync('config.json')resolves againstprocess.cwd(), not the script's folder. Usepath.join(__dirname, 'config.json'). npmscripts -ENOENT ... package.jsonmeans you rannpmfrom the wrong directory.spawn ENOENT-child_process.spawn('somecmd')cannot find the program. The binary is not installed or not onPATH.- Case differences -
Logs/andlogs/are different folders on Linux, even if they are the same on macOS or Windows.
Where to put the fix in production
Choose one owner for each directory the app needs: the app's startup code,
the setup script that installs it, a RUN mkdir -p line in the Dockerfile,
or a LogsDirectory= setting in a systemd unit. Do not depend on a folder
that someone created manually in the past.
Do this project yourself. It's ready in a live cloud workspace in your browser - when you finish, it lands on your portfolio.
Start this project - freeWhat you'll practice
- Reading the code, syscall, and path fields of a Node.js ENOENT error
- Creating a missing directory safely with mkdir -p
- Using fs.mkdirSync with recursive true so the app creates its own folders
FAQ
What does ENOENT: no such file or directory mean in Node.js?
ENOENT means the path does not exist. Node.js includes the exact path and the system call (open, stat, scandir) in the error so you can see what it tried.
Why do I get ENOENT when writing a file in Node.js?
Node.js creates a missing file when you write, but it does not create missing parent folders. Create the folder first with fs.mkdirSync(dir, { recursive: true }) or mkdir -p.
What is spawn ENOENT?
It means child_process.spawn could not find the program you asked it to run. Install it, or use the full path to the binary, or check that it is on PATH.
Is mkdir -p safe to run more than once?
Yes. It creates any missing folders and does nothing if they already exist, so it is safe in setup scripts and Dockerfiles.
Keep learning
Do this project yourself. It's ready in a live cloud workspace in your browser - when you finish, it lands on your portfolio.
Start this project - free