Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Node.js throws EISDIR when it reads a path

ErrorError: EISDIR: illegal operation on a directory, read

Short answer

EISDIR means Node.js was asked to read a file but the path points to a directory. On macOS, Linux and Windows fs.readFile rejects directories with this error. Print the path, check it with fs.stat, and fix the code that built it: usually an empty value, a folder given instead of a file, or a loop that visits folders.

  • Applies to Node.js 22, Node.js 24
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Log the exact path before the failing call and ask the file system what it is. The output tells you whether you hold a directory.

    node -e "const fs=require('node:fs'); const p=process.argv[1]; console.log(JSON.stringify(p), fs.statSync(p).isDirectory())" ./the/path/you/read
  2. 2Match what you see, then jump to its fix

    1. 1EISDIR: illegal operation on a directory, read <folder path>The path is a folder, not a fileFix
    2. 2EISDIR appears for a path that was supposed to be a file name, for example when an argument or environment variable was not setAn empty or missing value became a path to the current folderFix

What does the error look like?

  • Error: EISDIR: illegal operation on a directory, read
  • EISDIR

Cause 1 of 2

The path is a folder, not a file

1Check

Node.js documents that fs.readFile on a directory returns an error on macOS, Linux and Windows. Open the line in your stack trace and see which folder name is passed in.

node -e "const fs=require('node:fs'); console.log(fs.statSync('./uploads').isDirectory())"

2Fix

Point at the file inside the folder, or list the folder with readdir and read only the entries that are files.

const fs = require('node:fs');
const path = require('node:path');

const dir = './uploads';
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
  if (entry.isFile()) {
    const text = fs.readFileSync(path.join(dir, entry.name), 'utf8');
    console.log(entry.name, text.length);
  }
}

3Verify

Run the script again. It reads each file and skips folders without throwing.

node read-files.js

Confirmed by nodejs.org

Did this fix it?

Cause 2 of 2

An empty or missing value became a path to the current folder

1Check

path.join ignores empty segments and returns '.' when everything is empty, and path.resolve with no usable segments returns the current working directory. A blank variable therefore turns into a valid folder path instead of an error.

node -e "const path=require('node:path'); console.log(path.join(''), path.resolve(''))"

2Fix

Check the input before building the path and stop with a clear message when it is empty.

const path = require('node:path');

const name = process.argv[2];
if (!name) {
  console.error('Usage: node app.js <file name>');
  process.exit(1);
}
const file = path.resolve(process.cwd(), name);

3Verify

Run it with no argument and with a real file name. The first prints the usage line, the second reads the file.

node app.js
node app.js notes.txt

Confirmed by nodejs.org

Did this fix it?

How do I stop it coming back?

  • Validate every path that comes from user input, arguments or environment variables before you pass it to fs.
  • Use readdir with withFileTypes when you walk a folder, and read only entries where isFile() is true.
  • Log the resolved absolute path in your error handler so the folder name is in the log, not only the error code.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.