# mktemp

> create temporary files and directories

Latest version **2.0.4** (published 2026-08-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install mktemp
pnpm add mktemp
yarn add mktemp
bun add mktemp
```

## Health

**Score 75/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.0.4 |
| Published | 2026-08-24 |
| First published | 2013-01-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | 20 \|\| 22 \|\| 24 \|\| 26 |
| Dependencies | 0 |
| Unpacked size | 53.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 10 |
| Author | sasa+1 |
| Maintainers | sasaplus1 |

## Links

- npm: https://www.npmjs.com/package/mktemp
- Repository: https://github.com/sasaplus1/mktemp
- Homepage: https://github.com/sasaplus1/mktemp#readme
- Issues: https://github.com/sasaplus1/mktemp/issues
- npm.io page: https://npm.io/package/mktemp

## Recent versions

- 2.0.4 (latest) — 2026-08-24
- 2.0.3 — 2026-04-23
- 2.0.2 — 2025-09-13
- 2.0.1 — 2025-06-21
- 2.0.0 — 2025-06-21
- 1.0.1 — 2023-05-03
- 1.0.0 — 2019-05-27
- 0.4.0 — 2015-02-15
- 0.3.5 — 2014-06-24
- 0.3.4 — 2013-11-23
- 0.3.3 — 2013-11-13
- 0.3.2 — 2013-09-08
- 0.3.1 — 2013-06-19
- 0.3.0 — 2013-04-06
- 0.2.1 — 2013-03-28
- … 2 more at https://npm.io/package/mktemp/versions

## README

# mktemp

create temporary files and directories

## Installation

```sh
$ npm install mktemp
```

## Usage

```js
import * as os from 'node:os';
import * as path from 'node:path';
import * as mktemp from 'mktemp';

const tempDir = os.tmpdir();

mktemp.createFile(path.join(tempDir, 'XXXXX.txt'), function (err, path) {
  if (err) throw err;
  // path match a /^[\da-zA-Z]{5}\.txt$/
  console.log(path);
});

// return value match a /^[\da-zA-Z]{5}\.tmp$/
mktemp.createFileSync(path.join(tempDir, 'XXXXX.tmp'));

mktemp.createDir(path.join(tempDir, 'XXXXXXX'), function (err, path) {
  if (err) throw err;
  // path match a /^[\da-zA-Z]{7}$/
  console.log(path);
});

// return value match a /^XXX-[\da-zA-Z]{3}$/
mktemp.createDirSync(path.join(tempDir, 'XXX-XXX'));
```

if support Promise, can use Promise style.

```js
import * as mktemp from 'mktemp';

mktemp
  .createFile('XXXXX.txt')
  .then(function (path) {
    // path match a /^[\da-zA-Z]{5}\.txt$/
    console.log(path);
  })
  .catch(function (err) {
    console.error(err);
  });

mktemp
  .createDir('XXXXX')
  .then(function (path) {
    // path match a /^[\da-zA-Z]{5}$/
    console.log(path);
  })
  .catch(function (err) {
    console.error(err);
  });
```

mktemp functions are replace to random string from placeholder "X" in template. see example:

```js
mktemp.createFileSync('XXXXXXX'); // match a /^[\da-zA-Z]{7}$/
mktemp.createFileSync('XXX.tmp'); // match a /^[\da-zA-Z]{3}\.tmp$/
mktemp.createFileSync('XXX-XXX'); // match a /^XXX-[\da-zA-Z]{3}$/
```

## Functions

### createFile(template[, mode = 0o600[, callback]])

- `template`
  - `String` - filename template
- `mode`
  - `Number` - file permission mode (default: `0o600`)
- `callback`
  - `function(err, path)` - callback function
    - `err` : `Error|Null` - error object
    - `path` : `String` - path

create blank file of unique filename. return Promise if callback is not passed.

### createFileSync(template[, mode = 0o600])

- `template`
  - `String` - filename template
- `mode`
  - `Number` - file permission mode (default: `0o600`)
- `return`
  - `String` - path

sync version createFile.

### createDir(template[, mode = 0o700[, callback]])

- `template`
  - `String` - dirname template
- `mode`
  - `Number` - directory permission mode (default: `0o700`)
- `callback`
  - `function(err, path)` - callback function
    - `err` : `Error|Null` - error object
    - `path` : `String` - path

create directory of unique dirname. return Promise if callback is not passed.

### createDirSync(template[, mode = 0o700])

- `template`
  - `String` - dirname template
- `mode`
  - `Number` - directory permission mode (default: `0o700`)
- `return`
  - `String` - path

sync version createDir.

## License

The MIT license.

---
_Source: https://npm.io/package/mktemp · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
