Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Writing a file in Node.js when its parent folder does not exist yet

ErrorENOENT (No such file or directory) from fs.writeFile

Short 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.

  • Applies to Node.js 22, Node.js 24
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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')))"
  2. 2Match what you see, then jump to its fix

    1. 1ENOENT when writing out/reports/2026/data.json while out/reports/2026 is missingA folder in the path does not existFix
    2. 2EEXIST on the second run of the scriptmkdir is called without recursive and fails on a second runFix

What does the error look like?

  • ENOENT (No such file or directory) from fs.writeFile
  • EEXIST (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/2026

2Fix

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.json

Confirmed by nodejs.org

Did this fix it?

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 existed

3Verify

Run the script twice. The second run must not throw.

node make-folder.mjs
node make-folder.mjs

Confirmed by nodejs.org

Did this fix it?

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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.