# tiny-di

> A tini tiny dependency injection container for node/io.js applications

Latest version **2.1.1** (published 2020-01-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install tiny-di
pnpm add tiny-di
yarn add tiny-di
bun add tiny-di
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.1 |
| Published | 2020-01-29 |
| First published | 2015-01-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 41.5 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | Dennis Saenger |
| Maintainers | ds82 |
| Keywords | dependency, injection, ioc, guice |

## Links

- npm: https://www.npmjs.com/package/tiny-di
- Repository: https://github.com/ds82/tiny-di
- Homepage: https://github.com/ds82/tiny-di#readme
- Issues: https://github.com/ds82/tiny-di/issues
- npm.io page: https://npm.io/package/tiny-di

## Dependencies (2)

- [@babel/runtime](https://npm.io/package/@babel/runtime.md) 7.6
- [@babel/polyfill](https://npm.io/package/@babel/polyfill.md) 7.0.0

## Recent versions

- 2.1.1 (latest) — 2020-01-29
- 3.0.0-beta8 (beta) — 2020-01-30
- 3.0.0-beta7 — 2020-01-28
- 3.0.0-beta6 — 2020-01-07
- 3.0.0-beta5 — 2020-01-07
- 3.0.0-beta3 — 2020-01-07
- 3.0.0-beta2 — 2020-01-06
- 3.0.0-beta1 — 2020-01-06
- 3.0.0-beta0 — 2019-11-22
- 2.1.0 — 2019-10-25
- 2.0.0 — 2019-10-24
- 1.0.2 — 2019-02-25
- 1.0.1 — 2018-12-11
- 1.0.0 — 2018-12-06
- 0.4.3 — 2018-12-06
- … 15 more at https://npm.io/package/tiny-di/versions

## README

# tiny-di [![Build Status](https://secure.travis-ci.org/ds82/tiny-di.svg)](http://travis-ci.org/ds82/tiny-di) [![Coverage Status](https://coveralls.io/repos/ds82/tiny-di/badge.svg?branch=master)](https://coveralls.io/r/ds82/tiny-di?branch=master)

[![npm](https://nodei.co/npm/tiny-di.png?downloads=true&stars=true)](https://nodei.co/npm/tiny-di/)

A tini tiny dependency injection container for node.js/io.js

# example

```javascript
// main.js
var tiny = require('tiny-di')();

// `require` the module now and bind it to `app`
tiny.bind('app').load('lib/app');

var something = require('something');
something.setup();

// bind an existing object to `something`
tiny.bind('something').to(something);

// layz bind
// this module is loaded as soon as somebody requests the binding `another`
tiny.bind('another').lazy('v1/another');

// use namespaces
tiny.ns('some/ns').to('./some/local/dir/');
tiny.get('some/ns/foo'); // will require('./some/local/dir/foo')

// use custom providers
tiny.provide('Something').by(somethingProvider);

// this function is called whenever a module requires `Somehing`
function somethingProvider(env, injector) {
  return 'I was required by ' + env.binding;
}

// tiny-di comes with built-in resolver which tries
// to cover basic use cases.
// if you need a more advanced resolving of your deps, you can
// set a custom resolver
// have a look at the built-in resolver before doing this!
tiny.setResolver(function(file) {
  return require(file);
});


...
```

```javascript
// module
module.exports = Module;

Module.$inject = ['app', 'something', 'another'];
function Module(app, something, another) {

  // use the constructor pattern for your modules/libs/classes..
  if (!(this instanceof Module)) {
    return new Module(app, something, another);
  }

  var self = this;

  // app, something and another are injected :)
  ...
}

```

## advanced $inject configuration

```javascript
// module
module.exports = Module;

Module.$inject = {
  deps: ['app', 'something', 'another'],
  callAs: 'class' // or: 'function' (default is 'function')
};
function Module(app, something, another) {

  // callAs='class' tells the injector to instantiate a new class
  // now you don't need to use the constructor pattern. yah!

  var self = this;

  // (this instanceof Module) -> true!
}
```

# examples

look at the `example`-folder for a simple howto

# develop

```javascript
npm install
npm run watch

```

# tests

Test coverage is quite good at the moment (see badge above).


```javascript
npm install
npm test
```

# license

MIT

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