fs.mkdir()
Creates a directory.
Syntax
await mkdir(path[, options]) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
path | string | Buffer | URL | Yes | The directory path to create. |
options | object | integer | No | A mode integer, or an object with `recursive` (create parent directories as needed) and `mode`. |
Returns
Promise<string | undefined> — With recursive, resolves with the first directory path created, else undefined.
Examples
import { mkdir } from 'node:fs/promises';
await mkdir('./data/cache', { recursive: true });
console.log('ready');
Output
ready
import { mkdir } from 'node:fs/promises';
try {
await mkdir('./logs');
} catch (err) {
if (err.code !== 'EEXIST') throw err;
}
Notes
Without `recursive: true`, creating an existing directory throws
`EEXIST` and a missing parent throws `ENOENT`. With `recursive: true`
an existing directory is a no-op, making it the safe default.