Fixing 'Error: EISDIR: illegal operation on a directory, read' in Node.js

beginner๐Ÿ’š Node.js2026-04-27| Node.js (all versions), occurs on Windows, macOS, and Linux environments when using the fs module or build tools like Gulp, Webpack, or Vite.

Error Message

Error: EISDIR: illegal operation on a directory, read
#nodejs#fs#javascript#backend

The ContextYou're likely in the middle of processing a folder or running a build script when the process suddenly halts. The terminal throws a blunt message: Error: EISDIR: illegal operation on a directory, read. This happens because your code is trying to open a directory and read its contents as if it were a single file.

I usually see this when a developer loops through a folder containing subdirectories, or when a configuration variable points to /uploads/ instead of /uploads/image.png. Node.js expects a file, but it found a folder instead.

Why this happensEISDIR stands for "Error, Is Directory." At the system level, reading a directory requires different permissions and methods than reading a file. Functions like fs.readFile() or fs.createReadStream() expect a file descriptor. If you pass a path to a folder, the operating system rejects the request. You simply can't read a folder's raw binary data the same way you'd read a .txt or .json file.

Debug ProcessThe first step is pinpointing the exact path causing the crash. If your stack trace is vague, add a log right before the failing operation to see what the variable actually holds.

const filePath = path.join(__dirname, 'data', fileName);
console.log('Attempting to read path:', filePath);
const data = fs.readFileSync(filePath);

Check the output carefully. You might find that fileName is undefined, or the path ends abruptly at a folder name. If the error originates from a tool like Webpack or Vite, look at your entry points. A common mistake is pointing a source map or an asset include to a directory rather than a specific file.

Solution 1: Use withFileTypes for Better FilteringWhen looping through a directory with dozens of items, don't assume every item is a file. The most efficient way to handle this in modern Node.js is using the withFileTypes option in fs.readdirSync. This allows you to check the file type without making extra system calls for every single item.

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

const dirPath = './data';
const entries = fs.readdirSync(dirPath, { withFileTypes: true });

entries.forEach(entry => {
    const fullPath = path.join(dirPath, entry.name);
    
    if (entry.isDirectory()) {
        console.log(`Skipping directory: ${entry.name}`);
        return;
    }

    const content = fs.readFileSync(fullPath, 'utf8');
    console.log(`Successfully read: ${entry.name}`);
});

Solution 2: Refine Your Glob PatternsIf Gulp or a similar build tool triggers this error, your glob pattern is likely too broad. For instance, gulp.src('src/assets/*') tells the tool to grab everything in that folder, including subfolders. Gulp then tries to read those subfolders as files, leading to the EISDIR crash.

To fix this, be specific with file extensions:

// Problematic: picks up folders
gulp.src('src/assets/*')

// Safe: only picks up specific file types
gulp.src('src/assets/**/*.{png,jpg,svg}')

Solution 3: Fix Multer Path IssuesIn Express.js applications, this error often pops up when handling file uploads. Multer provides two main properties: destination (the folder) and path (the specific file). If you try to read the destination, the app will crash.

// This will trigger EISDIR
fs.readFile(req.file.destination, (err, data) => { ... });

// This is the correct way
fs.readFile(req.file.path, (err, data) => { ... });

VerificationWant to confirm a specific path is the culprit? Run this quick one-liner in your terminal:

node -e "require('fs').readFileSync('./your-suspect-path')"

If it returns the EISDIR error, you've confirmed that the path is a directory. Once you apply a filter or fix the path string, the command should either return the file's buffer or your code should skip it entirely.

Summary of Best Practices- Never assume folder content: Always check .isFile() or use withFileTypes when looping through directories.- Sanitize inputs: If a path comes from a user or a config file, verify it isn't a directory before calling read functions.- Use the path module: Avoid manual string concatenation. Use path.join() to prevent trailing slashes that might confuse Node.js into thinking a path is a directory.

Related Error Notes