# temp-write

> Write data to a random temporary file

Latest version **6.0.1** (published 2026-01-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install temp-write
pnpm add temp-write
yarn add temp-write
bun add temp-write
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 6.0.1 |
| Published | 2026-01-25 |
| First published | 2014-01-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=18 |
| Dependencies | 3 |
| Unpacked size | 6.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 108 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | temp, temporary, tempfile, file, filename, path, random, write, fs, string, buffer, uint8array, stream, data |

## Links

- npm: https://www.npmjs.com/package/temp-write
- Repository: https://github.com/sindresorhus/temp-write
- Homepage: https://github.com/sindresorhus/temp-write#readme
- Issues: https://github.com/sindresorhus/temp-write/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/temp-write

## Dependencies (3)

- [temp-dir](https://npm.io/package/temp-dir.md) ^3.0.0
- [is-stream](https://npm.io/package/is-stream.md) ^4.0.1
- [graceful-fs](https://npm.io/package/graceful-fs.md) ^4.2.11

## Alternatives

- [unionfs](https://npm.io/package/unionfs.md) — 2.2M weekly downloads
- [path-starts-with](https://npm.io/package/path-starts-with.md) — 35.9K weekly downloads
- [redzip](https://npm.io/package/redzip.md) — 1.2K weekly downloads
- [vscode-anymatch](https://npm.io/package/vscode-anymatch.md) — 848 weekly downloads
- [@ledgerhq/coin-filecoin](https://npm.io/package/@ledgerhq/coin-filecoin.md) — 793 weekly downloads

## Recent versions

- 6.0.1 (latest) — 2026-01-25
- 6.0.0 — 2024-07-26
- 5.0.0 — 2021-05-03
- 4.0.0 — 2019-04-28
- 3.4.0 — 2017-12-27
- 3.3.0 — 2017-05-11
- 3.2.0 — 2017-03-28
- 3.1.0 — 2017-02-17
- 3.0.0 — 2017-01-28
- 2.1.0 — 2015-11-14
- 2.0.1 — 2015-10-04
- 2.0.0 — 2015-10-03
- 1.1.2 — 2015-06-30
- 1.1.1 — 2015-05-07
- 1.1.0 — 2014-10-03
- … 6 more at https://npm.io/package/temp-write/versions

## README

# temp-write

> Write data to a random temporary file

## Install

```sh
npm install temp-write
```

## Usage

```js
import fs from 'node:fs';
import tempWrite from 'temp-write';

const filePath = tempWrite.sync('unicorn');
//=> '/var/folders/_1/tk89k8215ts0rg0kmb096nj80000gn/T/4049f192-43e7-43b2-98d9-094e6760861b'

fs.readFileSync(filePath, 'utf8');
//=> 'unicorn'

tempWrite.sync('unicorn', 'pony.png');
//=> '/var/folders/_1/tk89k8215ts0rg0kmb096nj80000gn/T/4049f192-43e7-43b2-98d9-094e6760861b/pony.png'

tempWrite.sync('unicorn', 'rainbow/cake/pony.png');
//=> '/var/folders/_1/tk89k8215ts0rg0kmb096nj80000gn/T/4049f192-43e7-43b2-98d9-094e6760861b/rainbow/cake/pony.png'
```

## API

### tempWrite(fileContent, filePath?)

Returns a `Promise` for the file path of the temporary file.

### tempWrite.sync(fileContent, filePath?)

Returns the file path of the temporary file.

#### fileContent

Type: `string | Uint8Array | stream.Readable`

The data to write to the temporary file. Streams are supported only with the async API.

#### filePath

Type: `string`\
Examples: `'img.png'` `'foo/bar/baz.png'`

Optionally specify a file path which is appended to the random path.

## Related

- [tempy](https://github.com/sindresorhus/tempy) - Get a random temporary file or directory path

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