Writing a file in Node.js when its parent folder does not exist yet
ENOENT (No such file or directory) from fs.writeFileShort answer
fs.writeFile creates the file, not the folders above it. If any folder in the path is missing, Node.js rejects with ENOENT. Create the parent path first with fs.mkdir and the recursive option, then write. With recursive set to true, mkdir also makes missing parents and does not fail when the folder already exists.
Fix it fast
1Find out which cause you have
Print the folder part of the path and check whether it exists. If this prints false, the cause is the first one below.
node -e "const fs=require('node:fs');const p=require('node:path');console.log(fs.existsSync(p.dirname('out/reports/2026/data.json')))"2Match what you see, then jump to its fix
What does the error look like?
ENOENT (No such file or directory) from fs.writeFileEEXIST (File exists) from fs.mkdir
Cause 1 of 2
A folder in the path does not exist
1Check
ENOENT means a component of the path could not be found. writeFile only creates the last part, the file itself, so every folder before it must already be there.
ls -ld out out/reports out/reports/20262Fix
Create the parent path with mkdir and recursive set to true, then write the file. path.dirname gives the folder part of a file path.
import { mkdir, writeFile } from 'node:fs/promises';
import { dirname } from 'node:path';
export async function writeFileDeep(file, data) {
await mkdir(dirname(file), { recursive: true });
await writeFile(file, data);
}
await writeFileDeep('out/reports/2026/data.json', '{}');3Verify
The file now exists at the full path.
cat out/reports/2026/data.jsonConfirmed by nodejs.org
Cause 2 of 2
mkdir is called without recursive and fails on a second run
1Check
Without the recursive option, calling mkdir on a folder that already exists rejects. The same call also cannot build a nested path in one go. Look at how your code calls it.
grep -rn "mkdir" src/2Fix
Pass { recursive: true }. The docs say a rejection for an existing directory happens only when recursive is false, and that with recursive true the promise resolves with the first directory it created.
import { mkdir } from 'node:fs/promises';
const first = await mkdir('out/reports/2026', { recursive: true });
console.log(first); // path of the first folder created, or undefined if all existed3Verify
Run the script twice. The second run must not throw.
node make-folder.mjs
node make-folder.mjsConfirmed by nodejs.org
How do I stop it coming back?
- Wrap folder creation and writing in one small helper so no call site forgets the mkdir.
- Build the target path in one place and use path.dirname on it instead of hand-cutting strings.
- Check error.code, not the message text, when you catch file errors.
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