# @loopback/context

> Facilities to manage artifacts and their dependencies in your Node.js applications. The module exposes TypeScript/JavaScript APIs and decorators to register artifacts, declare dependencies, and resolve artifacts by keys. It also serves as an IoC container

Latest version **8.0.15** (published 2026-08-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install @loopback/context
pnpm add @loopback/context
yarn add @loopback/context
bun add @loopback/context
```

## Health

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

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

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 8.0.15 |
| Published | 2026-08-18 |
| First published | 2017-04-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | 20 \|\| 22 \|\| 24 |
| Dependencies | 7 |
| Unpacked size | 647.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5106 |
| Author | IBM Corp. and LoopBack contributors |
| Maintainers | rfeng, rmg, dhmlau, theprez, frbuceta, marioestradarosa, achrinza |
| Keywords | LoopBack, IoC, Inversion, Control, Container, Decorators, Context |

## Links

- npm: https://www.npmjs.com/package/@loopback/context
- Repository: https://github.com/loopbackio/loopback-next
- Homepage: https://github.com/loopbackio/loopback-next#readme
- Issues: https://github.com/loopbackio/loopback-next/issues
- npm.io page: https://npm.io/package/@loopback/context

## Dependencies (7)

- [uuid](https://npm.io/package/uuid.md) ^14.0.1
- [debug](https://npm.io/package/debug.md) ^4.4.3
- [tslib](https://npm.io/package/tslib.md) ^2.8.1
- [hyperid](https://npm.io/package/hyperid.md) ^3.3.0
- [p-event](https://npm.io/package/p-event.md) ^4.2.0
- [@types/debug](https://npm.io/package/@types/debug.md) ^4.1.13
- [@loopback/metadata](https://npm.io/package/@loopback/metadata.md) ^8.0.15

## Alternatives

- [localforage](https://npm.io/package/localforage.md) — 6.2M weekly downloads
- [localforage-observable](https://npm.io/package/localforage-observable.md) — 30.8K weekly downloads
- [@y/y](https://npm.io/package/@y/y.md) — 30.1K weekly downloads
- [@metaobjectsdev/render](https://npm.io/package/@metaobjectsdev/render.md) — 3.5K weekly downloads
- [@ledgerhq/coin-algorand](https://npm.io/package/@ledgerhq/coin-algorand.md) — 1.1K weekly downloads

## Recent versions

- 8.0.15 (latest) — 2026-08-18
- 0.12.0 (dp3) — 2018-07-20
- 0.8.0 (dp2) — 2018-04-16
- 4.0.0-alpha.21 (dp1) — 2017-11-29
- 8.0.14 — 2026-07-16
- 8.0.13 — 2026-06-11
- 8.0.12 — 2026-05-12
- 8.0.11 — 2026-04-14
- 8.0.10 — 2026-03-11
- 8.0.9 — 2026-02-10
- 8.0.8 — 2026-01-12
- 8.0.7 — 2025-12-09
- 8.0.6 — 2025-11-11
- 8.0.5 — 2025-10-15
- 8.0.4 — 2025-09-10
- … 204 more at https://npm.io/package/@loopback/context/versions

## README

# @loopback/context

This module provides facilities to manage artifacts and their dependencies using
`Context` in your Node.js applications. It can be used independent of the
LoopBack framework.

## Overview

The `@loopback/context` package exposes TypeScript/JavaScript APIs and
decorators to register artifacts, declare dependencies, and resolve artifacts by
keys. The `Context` also serves as an
[IoC container](https://en.wikipedia.org/wiki/Inversion_of_control) to support
[dependency injection](https://en.wikipedia.org/wiki/Dependency_injection).

`Context` and `Binding` are the two core concepts. A context is a registry of
bindings and each binding represents a resolvable artifact by the key.

- Bindings can be fulfilled by a constant, a factory function, a class, or a
  provider.
- Bindings can be grouped by tags and searched by tags.
- Binding scopes can be used to control how a resolved binding value is shared.
- Bindings can be resolved synchronously or asynchronously.
- Provide `@inject` and other variants of decorators to express dependencies.
- Support Constructor, property, and method injections.
- Allow contexts to form a hierarchy to share or override bindings.
- Track bindings and injections during resolution to detect circular
  dependencies.

## Installation

```sh
npm install --save @loopback/context
```

## Basic use

### Locate an artifact by key

```js
const Context = require('@loopback/context').Context;
const ctx = new Context();
ctx.bind('hello').to('world'); // BindingKey='hello', BindingValue='world'
const helloVal = ctx.getSync('hello');
console.log(helloVal); // => 'world'
```

The binding can also be located asynchronously:

```ts
const helloVal = await ctx.get('hello');
console.log(helloVal); // => 'world'
```

### Dependency injection using context

```ts
import {Context, inject} from '@loopback/context';
const ctx = new Context();

// bind 'greeting' to 'Hello' as the constant value
ctx.bind('greeting').to('Hello');

class HelloController {
  constructor(
    // injecting the value bound to `greeting` using context
    @inject('greeting') private greeting: string,
  ) {}

  greet(name) {
    return `${this.greeting}, ${name}`;
  }
}

// Bind 'HelloController' to class HelloController
ctx.bind('HelloController').toClass(HelloController);

async function hello() {
  // Get an instance of HelloController
  const helloController = await ctx.get<HelloController>('HelloController');
  // helloController now has the `greeting` property injected with `Hello`
  console.log(helloController.greet('John')); // => Hello, John
}

hello();
```

For additional information, please refer to the
[Context page](http://loopback.io/doc/en/lb4/Context.html).

## Examples

To learn more about advanced features, check out standalone examples at
[`@loopback/example-context`](https://github.com/loopbackio/loopback-next/tree/master/examples/context).

Use the following command to download the example project to try out:

```sh
lb4 example context
```

## Contributions

- [Guidelines](https://github.com/loopbackio/loopback-next/blob/master/docs/CONTRIBUTING.md)
- [Join the team](https://github.com/loopbackio/loopback-next/issues/110)

## Tests

Run `npm test` from the root folder.

## Contributors

See
[all contributors](https://github.com/loopbackio/loopback-next/graphs/contributors).

## License

MIT

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