Node.js throws EISDIR when it reads a path
Error: EISDIR: illegal operation on a directory, readShort 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.
Fix it fast
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/read2Match what you see, then jump to its fix
What does the error look like?
Error: EISDIR: illegal operation on a directory, readEISDIR
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.jsConfirmed by nodejs.org
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.txtConfirmed by nodejs.org
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 fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack 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 fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix