Safely creating new directories using the Node.js File System API

A usual way to create a directory in Node.js is:

const fs = require('fs')

fs.mkdir('/path/to/new/directory', (err) => {
  // Callback code
})

If the /path/to/new/directory directory already exists, this will fail with the following error:

Error: EEXIST: file already exists, mkdir '/path/to/new/directory'

If the /path/to/new directory (the parent of the directory that we are trying to create) does not exist, this will also fail:

Error: ENOENT: no such file or directory, mkdir '/path/to/new/directory'

In Node.js 10.12.0 and above, it is possible to have these two cases handled automatically by passing the { recursive: true } option to mkdir:

const fs = require('fs')

fs.mkdir('/path/to/new/directory', { recursive: true }, (err) => {
  // Callback code
})

This code will create the /path/to/new/directory directory at the desired destination regardless of whether /path/to/new, /path/to, and /path exist. If the parent directory (or directories) doesn't exist, it will recursively create them. It also won't return an error if /path/to/new/directory already exists.

Importantly, the { recursive: true } option makes the mkdir calls idempotent, meaning that if you execute the same code over again, nothing will change and you won't get errors such as "file already exists".

Note that mkdir can still fail for a number of other reasons such as insufficient permissions, full disk, and so on.

The { recursive: true } option for mkdir was introduced in Node.js 10.12.0 and is not supported in earlier versions. Various third-party libraries can be used in place of native mkdir that offer the same functionality. These include mkdirp, fs-extra, make-dir, and others.

See also

Leskoff wyvern IPA Transcription Poster image
wyvern IPA Transcription Poster
$14.99

A poster featuring the phonetic transcription of "wyvern" in the International Phonetic Alphabet (IPA).

Leskoff Caffeine Molecule Poster image
Caffeine Molecule Poster, Ball-and-Stick Model, English-Labeled
$19.99

A poster featuring the ball-and-stick model of the caffeine molecule.

Leskoff Square Function Poster image
Square Function Poster, English-Labeled
$13.99

A poster featuring a plot of the square function.

Leskoff Quadratic Formula Poster image
Quadratic Formula Poster, English-Labeled
$16.99

A poster featuring the quadratic formula.

How to install Node.js and npm on a Mac using Homebrew

Homebrew is a package manager for macOS. This guide will help you install Node.js and npm using a Homebrew formula.

Walking a directory in Node.js

How to list all files recursively within a directory tree in Node.js?

Receiving email with Node.js

How to receive email in Node.js?

Fonts pronunciation guide

A pronunciation guide for the names of fonts.

Icelandic Keyboard

An on-screen/virtual Icelandic keyboard for typing Icelandic text.

All prices listed are in United States Dollars (USD). Visual representations of products are intended for illustrative purposes. Actual products may exhibit variations in color, texture, or other characteristics inherent to the manufacturing process. The products' design and underlying technology are protected by applicable intellectual property laws. Unauthorized reproduction or distribution is prohibited.