# @pyriter/anchorjs

> Dependency injection for Node

Latest version **3.3.0** (published 2025-10-25) · GPL license · 0 weekly downloads

## Install

```sh
npm install @pyriter/anchorjs
pnpm add @pyriter/anchorjs
yarn add @pyriter/anchorjs
bun add @pyriter/anchorjs
```

## Health

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

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

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 3.3.0 |
| Published | 2025-10-25 |
| First published | 2020-05-31 |
| Weekly downloads | 0 |
| License | GPL |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 50.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Phong Vuong |
| Maintainers | pyriter.io |
| Keywords | dependency injection, inversion of control, doj, ioc |

## Links

- npm: https://www.npmjs.com/package/@pyriter/anchorjs
- Repository: https://github.com/pyriter/anchorjs
- Homepage: https://github.com/pyriter/anchorjs#readme
- Issues: https://github.com/pyriter/anchorjs/issues
- npm.io page: https://npm.io/package/@pyriter/anchorjs

## Recent versions

- 3.3.0 (latest) — 2025-10-25
- 3.1.1 — 2024-08-03
- 3.1.0 — 2024-08-02
- 3.0.16 — 2024-05-19
- 3.0.15 — 2024-02-02
- 3.0.14 — 2024-02-02
- 3.0.13 — 2024-02-02
- 3.0.12 — 2024-02-02
- 3.0.11 — 2024-01-29
- 3.0.10 — 2024-01-29
- 3.0.7 — 2023-11-30
- 3.0.5 — 2023-11-25
- 3.0.4 — 2023-11-25
- 3.0.3 — 2023-11-24
- 3.0.1 — 2023-11-24
- … 12 more at https://npm.io/package/@pyriter/anchorjs/versions

## README

# Anchorjs

[![Patreon](https://img.shields.io/badge/Patreon-F96854?style=for-the-badge&logo=patreon&logoColor=white)](https://www.patreon.com/Pyriter)

Dependency injection for Nodejs.

## Install

```bash
npm install @pyriter/anchorjs
```

## Features

- Seamless imports with your IDE (via typescript)
- Allows named dependency
- Order of $install does not matter
- Infer dependency injection by object type
- Configurable duplicate key handling

## Usage

Import the $install function to add objects, functions, values to the anchor module

```typescript
import { $install } from '@pyriter/anchorjs';

declare type Foo = {
  bar: string
};
const myValue: Foo = {
  bar: "foobar"
};

$install<Foo>("foo", {
  provide: () => myValue,
  type: DependencyType.SINGLETON,
});
```

In a separate module or file use the $inject function to retrieve the desired object, function or value

```typescript
import { $inject } from '@pyriter/anchorjs';

const foo = $inject<Foo>('foo');
console.log(foo.bar); // prints "foobar" to the console
```

### Using dependency injection to inject data providers

One useful thing about dependency injection is to configure your objects and then have them injected to other objects on
creation

```typescript
import { $install, DependencyType } from '@pyriter/anchorjs';

type Credential = {
  username: string,
  password: string
};

const credentials = {
  username: "username",
  password: "password"
};

type DataSourceProvider = {
  getCredentials: () => Credential
}

$install<DataSourceProvider>('dataStoreProvider', {
  provide: () => new DataSourceProvider(credentials),
  type: DependencyType.FACTORY
});
```

Then in a separate file, you can create another object that uses these providers without knowing how to set them up

```typescript
import { $inject } from '@pyriter/anchorjs';

class MyActionController {
  constructor(readonly private dataSourceProvider = $inject<DataSourceProvider>("dataSourceProvider")) {
  }

  public async getCredentials(): Promise<DataItem> {
    return this.dataSourceProvider.getCredentials();
  }
}
```

Now you can create the MyActionController object without having to know how to create the 2 dataSources

```typescript
const myActionController = new MyActionController();
```

# API

## $install

The $install function takes in 2 arguments: `key: string`, and `props: InjectProps`

### key

The key is a string value only (at this time)

### InjectProps

```typescript
type InjectProps = {
  provide: () => T,
  type: DependencyType
}
```

The type is used to determine if the provide function call be called for every $inject (Factory) or should the value be
retrieved by a lookup (Singleton). The default behavoir is `DependencyType.Factory`

## $inject

The $inject function is used to retrieve the desired object from the installation step. It takes in the return type and the key

```typescript
type $inject = (key: string)<T> => T;
```

## $configure

The $configure function allows you to modify the behavior of the dependency injection system.

```typescript
import { $configure } from '@pyriter/anchorjs';

$configure({ allowKeyOverridesOnInstall: true });
```

### Options

- `allowKeyOverridesOnInstall: boolean` - When `true`, allows installing the same key multiple times (latest wins). Defaults to `false` for backwards compatibility.

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