This error occurs when Node.js file system operations attempt to read, write, or manipulate a directory as if it were a regular file. The operation expects a file path but receives a directory path instead.
The EISDIR error (Error, Is Directory) is a file system error in Node.js that occurs when your code attempts to perform file-specific operations on a directory path. File operations like fs.readFile(), fs.readFileSync(), fs.writeFile(), and similar methods are designed to work exclusively with files, not directories. When Node.js encounters a directory path where it expects a file path, it throws this error to prevent invalid operations. This is a safety mechanism that prevents attempting to read directory metadata as file content or writing data to a directory structure. The error typically originates from the Node.js fs module at the system call level (readSync, writeSync, etc.) when the underlying operating system rejects the operation because the target is a directory, not a file.
Check the exact path being passed to the file system operation. Add logging to see what path is being used:
const filePath = './data'; // Is this a file or directory?
console.log('Attempting to read:', filePath);
try {
const content = fs.readFileSync(filePath, 'utf-8');
} catch (error) {
console.error('Error reading file:', error);
}Ensure the path points to an actual file with an extension (e.g., ./data/config.json not ./data).
Use fs.statSync() or fs.stat() to verify whether a path is a file or directory before attempting file operations:
const fs = require('fs');
const path = require('path');
const targetPath = './myfile';
try {
const stats = fs.statSync(targetPath);
if (stats.isDirectory()) {
console.error('Error: Path is a directory, not a file');
// Handle the directory case - perhaps read files within it
const files = fs.readdirSync(targetPath);
console.log('Files in directory:', files);
} else if (stats.isFile()) {
// Safe to read as a file
const content = fs.readFileSync(targetPath, 'utf-8');
console.log('File content:', content);
}
} catch (error) {
console.error('Path does not exist:', error);
}This prevents EISDIR errors by detecting directories before attempting file operations.
If the error occurs with config files or environment files, check for directories where files should be:
# Check if .env is a directory instead of a file
ls -la | grep .env
# If .env is a directory, remove it
rm -rf .env
# Create the proper .env file
touch .env
echo "NODE_ENV=development" >> .envSimilarly, check build tool configurations, package.json paths, and any file references in your project settings.
For web servers, ensure URLs properly map to files with extensions:
const http = require('http');
const fs = require('fs');
const path = require('path');
http.createServer((req, res) => {
let filePath = '.' + req.url;
// Add .html extension if no extension is present
if (path.extname(filePath) === '') {
filePath += '.html';
}
// Check if path is a file before reading
fs.stat(filePath, (err, stats) => {
if (err || stats.isDirectory()) {
res.writeHead(404);
res.end('File not found');
return;
}
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(500);
res.end('Server error');
} else {
res.writeHead(200);
res.end(data);
}
});
});
}).listen(8080);When processing multiple paths, filter out directories before file operations:
const fs = require('fs');
const path = require('path');
function processFiles(dirPath) {
const entries = fs.readdirSync(dirPath, { withFileTypes: true });
// Filter to only process files, not directories
const files = entries
.filter(entry => entry.isFile())
.map(entry => path.join(dirPath, entry.name));
files.forEach(filePath => {
try {
const content = fs.readFileSync(filePath, 'utf-8');
// Process file content
console.log(`Processed: ${filePath}`);
} catch (error) {
console.error(`Failed to read ${filePath}:`, error);
}
});
}
processFiles('./data');This ensures you only attempt to read files, avoiding EISDIR errors from subdirectories.
Platform-Specific Behavior: On most platforms (macOS, Linux, Windows), fs.readFile() will reject with EISDIR when given a directory path. However, on FreeBSD, it may return a representation of the directory's contents, leading to inconsistent behavior across environments.
Package Manager Errors: When npm or yarn throws EISDIR errors during installation, the issue is often caused by corrupted cache files or directories. Running npm cache clean --force or yarn cache clean can resolve these issues. Alternatively, deleting node_modules and package-lock.json/yarn.lock, then reinstalling can help.
Build Tool Conflicts: Bundlers like Metro, Webpack, and Vite may encounter EISDIR errors when their configuration inadvertently includes directories in the list of files to process. Check your include/exclude patterns and ensure source file globs don't match directories.
Cloud Storage Issues: Some users report EISDIR errors when running projects from cloud-synced directories (Google Drive, Dropbox, OneDrive). These sync services can create directory structures or permissions that confuse Node.js file operations. Moving the project to a local-only directory often resolves the issue.
Error Code vs. Error Name: In Node.js, this error may appear as both "EISDIR" (the POSIX error code) and "ERR_FS_EISDIR" (the Node.js error name). Both refer to the same underlying issue and should be handled identically.
Error: Listener already called (once event already fired)
EventEmitter listener already called with once()
Error: EACCES: permission denied, open '/root/file.txt'
EACCES: permission denied
Error: Invalid encoding specified (stream encoding not supported)
How to fix Invalid encoding error in Node.js readable streams
Error: EINVAL: invalid argument, open
EINVAL: invalid argument, open
TypeError: readableLength must be a positive integer (stream config)
TypeError: readableLength must be a positive integer in Node.js streams