Skip to main content

mkdir

function mkdir
Jump to headingmkdir(
path: PathLike,
options: MakeDirectoryOptions & { recursive: true; },
callback: (
err: ErrnoException | null,
path?: string,
) => void
,
): void

Asynchronously creates a directory.

The callback is given a possible exception and, if recursive is true, the first directory path created, (err[, path]).path can still be undefined when recursive is true, if no directory was created (for instance, if it was previously created).

The optional options argument can be an integer specifying mode (permission and sticky bits), or an object with a mode property and a recursive property indicating whether parent directories should be created. Calling fs.mkdir() when path is a directory that exists results in an error only when recursive is false. If recursive is false and the directory exists, an EEXIST error occurs.

import { mkdir } from 'node:fs';

// Create ./tmp/a/apple, regardless of whether ./tmp and ./tmp/a exist.
mkdir('./tmp/a/apple', { recursive: true }, (err) => {
  if (err) throw err;
});

On Windows, using fs.mkdir() on the root directory even with recursion will result in an error:

import { mkdir } from 'node:fs';

mkdir('/', { recursive: true }, (err) => {
  // => [Error: EPERM: operation not permitted, mkdir 'C:\']
});

See the POSIX mkdir(2) documentation for more details.

Parameters Jump to heading

Jump to headingoptions: MakeDirectoryOptions & { recursive: true; }
Jump to headingcallback: (
err: ErrnoException | null,
path?: string,
) => void

Return Type Jump to heading

void
Jump to headingmkdir(
path: PathLike,
options:
Mode
| (MakeDirectoryOptions & { recursive?: false | undefined; })
| null
| undefined
,
callback: NoParamCallback,
): void

Asynchronous mkdir(2) - create a directory.

Parameters Jump to heading

A path to a file. If a URL is provided, it must use the file: protocol.

Jump to headingoptions:
Mode
| (MakeDirectoryOptions & { recursive?: false | undefined; })
| null
| undefined

Either the file mode, or an object optionally specifying the file mode and whether parent folders should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to 0o777.

Return Type Jump to heading

void
Jump to headingmkdir(
path: PathLike,
options:
Mode
| MakeDirectoryOptions
| null
| undefined
,
callback: (
err: ErrnoException | null,
path?: string,
) => void
,
): void

Asynchronous mkdir(2) - create a directory.

Parameters Jump to heading

A path to a file. If a URL is provided, it must use the file: protocol.

Jump to headingoptions:
Mode
| MakeDirectoryOptions
| null
| undefined

Either the file mode, or an object optionally specifying the file mode and whether parent folders should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to 0o777.

Jump to headingcallback: (
err: ErrnoException | null,
path?: string,
) => void

Return Type Jump to heading

void
Jump to headingmkdir(
path: PathLike,
callback: NoParamCallback,
): void

Asynchronous mkdir(2) - create a directory with a mode of 0o777.

Parameters Jump to heading

A path to a file. If a URL is provided, it must use the file: protocol.

Return Type Jump to heading

void
Back to top