# hook-std

> Hook and modify stdout and stderr

Latest version **4.0.0** (published 2025-09-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install hook-std
pnpm add hook-std
yarn add hook-std
bun add hook-std
```

## Health

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

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

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 4.0.0 |
| Published | 2025-09-09 |
| First published | 2015-12-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=20 |
| Dependencies | 0 |
| Unpacked size | 12.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 55 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | hook, intercept, capture, modify, change, test, assert, check, std, stdio, stdout, stderr, console, log, process |

## Links

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

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads

## Recent versions

- 4.0.0 (latest) — 2025-09-09
- 3.0.0 — 2021-09-17
- 2.0.0 — 2019-04-05
- 1.2.0 — 2018-11-20
- 1.1.0 — 2018-07-17
- 1.0.1 — 2018-06-10
- 1.0.0 — 2018-06-08
- 0.4.0 — 2017-04-26
- 0.3.0 — 2017-02-18
- 0.2.0 — 2016-01-12
- 0.1.0 — 2015-12-13

## README

# hook-std

> Hook and modify stdout and stderr

## Install

```sh
npm install hook-std
```

## Usage

```js
import assert from 'node:assert';
import {hookStdout} from 'hook-std';

const promise = hookStdout(output => {
	promise.unhook();
	assert.strictEqual(output.trim(), 'unicorn');
});

console.log('unicorn');
await promise;
```

You can also unhook using the second `transform` method parameter:

```js
import assert from 'node:assert';
import {hookStdout} from 'hook-std';

const promise = hookStdout((output, unhook) => {
	unhook();
	assert.strictEqual(output.trim(), 'unicorn');
});

console.log('unicorn');
await promise;
```

Automatically collect output without a transform function:

```js
import {hookStdout} from 'hook-std';

const promise = hookStdout();
console.log('unicorn');
console.log('rainbow');
console.log(promise.output); // 'unicorn\nrainbow\n'
promise.unhook();
await promise;
```

## API

### hookStd(options?, transform?)

Hook streams in [`streams` option](#streams), or stdout and stderr if none are specified.

Returns a `Promise` with a `unhook()` method which, when called, unhooks both stdout and stderr and resolves the `Promise` with an empty result. The returned promise also has an `output` property to access the collected output.

When hooking multiple streams, the `output` property contains the interleaved output in the order the writes occurred:

```js
import {hookStd} from 'hook-std';

const promise = hookStd();
process.stdout.write('A');
process.stderr.write('B');
process.stdout.write('C');
console.log(promise.output); // 'ABC' (interleaved in order)
promise.unhook();
```

### hookStdout(options?, transform?)

Hook stdout.

Returns a `Promise` with a `unhook()` method which, when called, unhooks stdout and resolves the `Promise` with an empty result. The returned promise also has an `output` property to access the collected output.

### hookStderr(options?, transform?)

Hook stderr.

Returns a `Promise` with a `unhook()` method which, when called, unhooks stderr and resolves the `Promise` with an empty result. The returned promise also has an `output` property to access the collected output.

#### options

Type: `object`

##### silent

Type: `boolean`\
Default: `true`

Suppress stdout/stderr output.

##### once

Type: `boolean`\
Default: `false`

Automatically unhook after the first call.

##### streams

Type: `stream.Writable[]`\
Default: `[process.stdout, process.stderr]`

The [writable streams](https://nodejs.org/api/stream.html#stream_writable_streams) to hook. This can be useful for libraries allowing users to configure a writable stream to write to.

#### transform

Type: `Function`

Receives stdout/stderr as the first argument and the unhook method as the second argument. Return a string to modify it. Optionally, when in silent mode, you may return a `boolean` to influence the return value of `.write(…)`.

Output is automatically collected and can be accessed via the `output` property of the returned promise, regardless of whether a transform function is provided.

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