# context

> Lightweight context propagation for JavaScript and TypeScript. Create a scoped storage object, run code inside it, and read the active value anywhere down the call stack - without depending on React.

Latest version **4.0.18** (published 2026-10-03) · MIT license · 48.2K weekly downloads

## Install

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

## Health

**Score 85/100 (A)** — status: active.

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

## Facts

| | |
|---|---|
| Version | 4.0.18 |
| Published | 2026-10-03 |
| First published | 2014-05-11 |
| Weekly downloads | 48.2K |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 37.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 2668 |
| Author | ealush |
| Maintainers | ealush |

## Links

- npm: https://www.npmjs.com/package/context
- Repository: https://github.com/ealush/vest
- Homepage: https://github.com/ealush/vest#readme
- Issues: https://github.com/ealush/vest.git/issues
- npm.io page: https://npm.io/package/context

## Dependencies (1)

- [vest-utils](https://npm.io/package/vest-utils.md) ^2.0.18

## Recent versions

- 4.0.18 (latest) — 2026-10-03
- 4.0.18-nightly-20261003-b13b (nightly) — 2026-10-03
- 4.0.14-next-20260318-d8c2 (next) — 2026-03-18
- 3.0.33-dev-20241111-bedd (dev) — 2024-11-11
- 4.0.18-nightly-20261002-b13b — 2026-10-02
- 4.0.18-nightly-20260716-9eba — 2026-07-16
- 4.0.18-nightly-20260715-9eba — 2026-07-15
- 4.0.18-nightly-20260714-9eba — 2026-07-14
- 4.0.18-nightly-20260713-9eba — 2026-07-13
- 4.0.18-nightly-20260712-9eba — 2026-07-12
- 4.0.18-nightly-20260711-7e44 — 2026-07-11
- 4.0.18-nightly-20260710-7e44 — 2026-07-10
- 4.0.18-nightly-20260709-7e44 — 2026-07-09
- 4.0.18-nightly-20260708-7e44 — 2026-07-08
- 4.0.18-nightly-20260707-7e44 — 2026-07-07
- … 285 more at https://npm.io/package/context/versions

## README

# Context 🪆

Lightweight context propagation for JavaScript and TypeScript. Create a scoped storage object, run code inside it, and read the active value anywhere down the call stack - without depending on React.

- `createContext(defaultValue?)` – simplest option with a single value per run.
- `createCascade(initializer?)` – merges parent and child context objects, ideal for layered data.
- `bind` – capture context for later execution (useful with async callbacks).

## Installation

```bash
npm i context
```

## createContext

Create an isolated context with an optional default value used outside of a running scope.

```js
import { createContext } from 'context';

const requestContext = createContext({ requestId: undefined });

function handleRequest(requestId) {
  return requestContext.run({ requestId }, () => {
    logRequest();
    return doWork();
  });
}

function logRequest() {
  // Inside run → current context value
  console.log(requestContext.use().requestId);
}
```

**API:**

- `run(value, callback)` – activates the context for the callback. Nested runs temporarily override the value and restore it afterwards.
- `use()` – returns the current value or the default when no context is active.
- `useX(message?)` – like `use`, but throws if called outside of `run`, ignoring any default value.

## createCascade

`createCascade` composes context objects instead of replacing them. Each `run` merges the current value with the parent context, so shared properties flow down while children can add or override keys.

```js
import { createCascade } from 'context';

const userContext = createCascade();

userContext.run({ user: { id: 5, name: 'Ada' } }, () => {
  // child run inherits id/name and adds role
  userContext.run({ user: { role: 'admin' } }, () => {
    const ctx = userContext.use();
    // ctx.user => { id: 5, name: 'Ada', role: 'admin' }
  });
});
```

**API additions:**

- `use()` / `useX(message?)` – return the merged object for the current layer.
- `run(value, callback)` – merges `value` into the parent context without passing arguments to `callback`.
- `bind(value, callback)` – returns a function that executes `callback` inside a context seeded with `value`, preserving arguments when invoked later.

## TypeScript

Both factories are fully typed. Annotate your contexts for stricter checks:

```ts
const settings = createContext<{ locale: string }>();
const session = createCascade<{ user?: { id: string } }>();
```

## Notes

- Context is synchronous by design; reading it after an async boundary requires `bind` or wrapping the async call inside `run`.
- Each `run` call leaves the parent context untouched once the callback completes.

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