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.

Start this project - freeLive cloud workspace in your browser · lands on your portfolio
DevOps Engineernodejslinuxfile-system

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:

Writing a file vs reading a file

The fix depends on what the app was doing:

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

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 - free

What you'll practice

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

Restore a Misplaced Linux Config FileDevOps projectFree the Port: Your Server Won't Start (EADDRINUSE)DevOps projectFix a Docker Container That Exits ImmediatelyDevOps projectDevOps roadmapStep by step to hiredDevOps interview questionsSTAR answersAll DevOps projectsProjects hub

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
Start this project - free