← Back to Error Library
ENOENTNode.jsMay 2026

ENOENT: no such file or directory, open './config.json'

Error Message

terminal
Error: ENOENT: no such file or directory, open './config.json'
  at Object.openSync (node:fs:600:3)
  at Object.readFileSync (node:fs:468:35)

Root Cause

Relative paths in Node.js resolve from the **current working directory** (where the process was launched), not from the file's own location. Running the script from a different folder broke the path.

Fix

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

// Before (broken) — resolves from cwd
const config = readFileSync('./config.json', 'utf8')

// After (fixed) — resolves from this file's directory
const __dirname = dirname(fileURLToPath(globalThis._importMeta_.url))
const config = readFileSync(join(__dirname, 'config.json'), 'utf8')

Takeaway

./ resolves from where the *process* runs, not where the *file* lives. Use __dirname (CommonJS) or dirname(fileURLToPath(globalThis._importMeta_.url)) (ESM) to anchor paths to the file itself.

Node.jsFile SystemESM