← All fixes
Node: Error: ENOENT: no such file or directory, open (relative path)
#171Error
Error: ENOENT: no such file or directory, open './data/config.json'
Why it happens, and why this works
Relative paths in fs calls resolve against the process's current working directory, not the file the code is in. Running from another folder (npm scripts in a monorepo, a test runner, a serverless function) breaks them. On Vercel, files that aren't imported also aren't in the deployed bundle.
Fix
1. Build paths from the module's location: path.join(import.meta.dirname, "data", "config.json") (ESM, Node 20.11+) or path.join(__dirname, ...) (CommonJS). In Next.js, path.join(process.cwd(), "data", ...) is the project root. 2. Log path.resolve(thePath) to see where it's actually looking. 3. On Vercel/Next.js, include data files in the output: outputFileTracingIncludes in next.config, or import the JSON directly.
Did it work?
No reports yet0 worked0 didn't
Tried it yourself?
Related fixes
ENOENT reading a file that exists: relative path resolved from the wrong folder
#30Error: ENOENT: no such file or directory, open './data.json' (the file is right next to the script)
nodeVerifiedNo reports yet
FileNotFoundError for a file next to the script (path is relative to the current folder)
#79FileNotFoundError: [Errno 2] No such file or directory: 'data/config.json'
pythonVerifiedNo reports yet
TS2835: Relative import paths need explicit file extensions (moduleResolution node16/nodenext)
#42error TS2835: Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './math.js'?
typescriptnodeVerifiedNo reports yet