How to fix Error: ENOENT: no such file or directory, open 'file.txt' in Node.js

Node.jsBEGINNERMEDIUMUpdated Dec 9, 2025ยท 203 views

The ENOENT (Error NO ENTry) error occurs when Node.js attempts to access a file or directory that does not exist at the specified path. This is one of the most common filesystem errors and typically results from incorrect file paths, relative path confusion, or missing files. Understanding how Node.js resolves paths is essential to fixing this error permanently.

Quick fix

Fixes ENOENT

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

const filePath = './file.txt';

if (fs.existsSync(filePath)) {
  console.log('File exists at:', path.resolve(filePath));
} else {
  console.log('File does NOT exist at:', path.resolve(filePath));
  console.log('Current working directory:', process.cwd());
}
  1. Verify the file path and check if the file exists
  2. Use __dirname or __filename to construct absolute paths
  3. Always use the path module for cross-platform compatibility
  4. Create parent directories before writing files
node.js โ€” error
const fs = require('fs');
const path = require('path');
const filePath = './file.txt';
if (fs.existsSync(filePath)) {
console.log('File exists at:', path.resolve(filePath));
} else {
console.log('File does NOT exist at:', path.resolve(filePath));
console.log('Current working directory:', process.cwd());
}
Error: ENOENT: no such file or directory, open 'file.txt'

What this error means

ENOENT stands for "Error NO ENTry" and is a system-level error code indicating that the operating system cannot locate the file or directory at the specified path. When Node.js file system operations like fs.readFile(), fs.writeFile(), fs.access(), or fs.createReadStream() attempt to access a path, they ask the operating system to locate that file. If the OS cannot find it, it returns the ENOENT error code, which Node.js propagates to your application. This error is particularly common because Node.js resolves relative paths based on the current working directory (process.cwd()), not the location of the script file itself. This means a path like "./file.txt" may work when you run your script from one directory but fail when run from another, even though the code has not changed.

Typical symptoms

  • File read/write operations fail with "ENOENT: no such file or directory" error message
  • Application crashes when trying to access configuration files or data files
  • Script works when run from one directory but fails when run from another location
  • Error includes the full path that was attempted, showing where Node.js looked for the file
  • npm commands fail with ENOENT errors when package.json or node_modules are missing
  • File operations that worked locally fail in production or CI/CD environments
  • Operations on newly created files fail because parent directories do not exist

Common causes

  1. File or directory genuinely does not exist at the specified path - the most common cause
  2. Using relative paths without understanding that Node.js resolves them from the current working directory (process.cwd()), not from the script location
  3. Incorrect file path due to typos, wrong file extensions, or case sensitivity issues (especially on Linux/macOS)
  4. Path separator inconsistencies between Windows (backslashes) and Unix-like systems (forward slashes) when paths are hardcoded
  5. Attempting to write to a file in a directory that has not been created yet - fs operations do not create parent directories automatically
  6. Asynchronous timing issues where code attempts to access a file before it has been created by a previous operation
  7. Environment-specific paths that work locally but fail in production due to different directory structures
  8. Hidden characters or whitespace in filenames (like carriage returns) that make the filename appear correct but actually differ

How to fix "Error: ENOENT: no such file or directory, open 'file.txt'"

1Verify the file path and check if the file exists

First, confirm the exact path Node.js is trying to access by examining the error message. Then verify the file actually exists at that location. Use fs.existsSync() to programmatically check, or manually inspect the filesystem.

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

const filePath = './file.txt';

if (fs.existsSync(filePath)) {
  console.log('File exists at:', path.resolve(filePath));
} else {
  console.log('File does NOT exist at:', path.resolve(filePath));
  console.log('Current working directory:', process.cwd());
}

This will show you the absolute path Node.js is checking and help identify if the file is in a different location.

2Use __dirname or __filename to construct absolute paths

Replace relative paths with absolute paths constructed using __dirname (the directory of the current module) or __filename (the full path of the current file). This ensures paths work regardless of where the script is executed from.

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

// WRONG - relative path depends on working directory
// fs.readFileSync('./data/file.txt');

// CORRECT - absolute path relative to the script location
const filePath = path.join(__dirname, 'data', 'file.txt');
const content = fs.readFileSync(filePath, 'utf8');

For ES modules (using import instead of require), use import.meta.url:

javascript
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import { readFileSync } from 'fs';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const filePath = join(__dirname, 'data', 'file.txt');
const content = readFileSync(filePath, 'utf8');
3Always use the path module for cross-platform compatibility

Use path.join() or path.resolve() to combine path segments instead of string concatenation. This automatically handles the correct path separator for the operating system.

javascript
const path = require('path');

// WRONG - hardcoded separator breaks on Windows/Unix
// const filePath = './data/' + filename;
// const filePath = '.\\data\\' + filename; // Windows-specific

// CORRECT - works on all platforms
const filePath = path.join('data', filename);
const absolutePath = path.resolve(__dirname, 'data', filename);

path.join() combines segments with the OS separator, while path.resolve() creates an absolute path.

4Create parent directories before writing files

If you are creating a new file, ensure all parent directories exist first. Use fs.mkdirSync() with the recursive option to create the entire directory tree.

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

const filePath = path.join(__dirname, 'output', 'logs', 'app.log');
const dirPath = path.dirname(filePath);

// Create all parent directories if they don't exist
fs.mkdirSync(dirPath, { recursive: true });

// Now write the file
fs.writeFileSync(filePath, 'Log content', 'utf8');

The recursive: true option prevents errors if the directory already exists.

5Handle asynchronous operations correctly

If you are performing multiple file operations, ensure they complete in the correct order. Use callbacks, promises, or async/await to prevent race conditions where you try to read a file before it has been written.

javascript
const fs = require('fs').promises;
const path = require('path');

async function processFiles() {
  const filePath = path.join(__dirname, 'temp.txt');
  
  try {
    // Write file first
    await fs.writeFile(filePath, 'Data', 'utf8');
    
    // Now read it - guaranteed to exist
    const content = await fs.readFile(filePath, 'utf8');
    console.log(content);
  } catch (error) {
    console.error('File operation failed:', error);
  }
}

processFiles();
6Wrap file operations in try-catch blocks for graceful error handling

Always handle ENOENT errors gracefully so your application can recover or provide helpful error messages instead of crashing.

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

const filePath = path.join(__dirname, 'config.json');

try {
  const config = JSON.parse(fs.readFileSync(filePath, 'utf8'));
  console.log('Config loaded:', config);
} catch (error) {
  if (error.code === 'ENOENT') {
    console.error(`Configuration file not found at ${filePath}`);
    console.error('Please create the config file or check the path.');
    // Optionally create a default config
  } else {
    throw error; // Re-throw other errors
  }
}

Advanced notes

The ENOENT error can also occur in npm operations when package.json, node_modules, or specific package binaries are missing. Run npm install to restore missing packages. In Docker containers or CI/CD pipelines, ensure COPY or volume mount commands include the necessary files and that the working directory is set correctly with WORKDIR. When working with symbolic links, ENOENT can occur if the link target does not exist, even though the link itself exists - use fs.lstat() instead of fs.stat() to check the link itself. On case-sensitive filesystems (Linux, macOS), "File.txt" and "file.txt" are different files, while Windows treats them as the same. When debugging path issues in production, log process.cwd() and __dirname to understand the execution context. Some frameworks like Next.js change the working directory during build or runtime, which can affect relative path resolution. For complex path logic, consider using libraries like find-up or pkg-dir to locate files relative to package boundaries rather than the filesystem hierarchy.

Frequently asked questions

What does "ENOENT" mean?
ENOENT stands for "Error NO ENTry" and is a system-level error code indicating that the operating system cannot locate the file or directory at the specified path. createReadStream() attempt to access a path, they ask the operating system to locate that file. js propagates to your application.
How do I fix "ENOENT"?
The ENOENT (Error NO ENTry) error occurs when Node.js attempts to access a file or directory that does not exist at the specified path. This is one of the most common filesystem errors and typically results from incorrect file paths, relative path confusion, or missing files. Understanding how Node.js resolves paths is essential to fixing this error permanently.
What causes "ENOENT"?
File or directory genuinely does not exist at the specified path - the most common cause Using relative paths without understanding that Node.js resolves them from the current working directory (process.cwd()), not from the script location Incorrect file path due to typos, wrong file extensions, or case sensitivity issues (especially on Linux/macOS)
Is the "ENOENT" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading