Skip to main content
Version: 6.x

Getting started

Start by installing inversify and reflect-metadata:

npm install inversify reflect-metadata

Next, initialize your first container and add some bindings:

warning

Make sure to enable Experimental decorators and Emit Decorator Metadata options in your tsconfig.json to use this library.

import { Container, inject, injectable } from 'inversify';

@injectable()
class Katana {
public readonly damage: number = 10;
}

@injectable()
class Ninja {
constructor(
@inject(Katana)
public readonly katana: Katana,
) {}
}

const container: Container = new Container();

container.bind(Ninja).toSelf();
container.bind(Katana).toSelf();

const ninja: Ninja = container.get(Ninja);

console.log(ninja.katana.damage);

The @injectable decorator allows both Katana and Ninja classes to be used as container bindings. The @inject decorator provides metadata about Ninja dependencies, so the container knows that a Katana should be provided as the first argument of Ninja's constructor.

Bindings are provided through the Container API.

With these two steps, you are ready to initialize your very first ninja!