mdawar.dev

A blog about programming, Web development, Open Source, Linux and DevOps.

Create a Directory if It Does Not Exist in Node.js

Using fs.mkdir to create a directory will throw an error if it already exists.

We can use the recursive option to create a directory along with its parents as needed if it doesn’t already exist.

js
import fs from 'node:fs/promises';

// Parent directories will be creates as needed
await fs.mkdir('./path/to/dir', { recursive: true });