Skip to content

Getting started ​

Installation ​

Just install it using your favorite package manager.

Needle DI is published to NPM and JSR, and is also compatible with Deno.

bash
npm install @needle-di/core
bash
yarn add @needle-di/core
bash
pnpm install @needle-di/core
bash
deno add jsr:@needle-di/core

Transpiler settings ​

Needle DI uses native ECMAScript decorators, which are currently in stage 3 of the TC39 standardization process.

If you're using Deno, you can run your code as-is. However, when running on Node.js or in a browser, you might need to transpile your code first, as your runtime might not have implemented it yet.

Make sure to use ES2022 or lower as target:

json
{
  "compilerOptions": {
    "target": "ES2022"
  }
}
javascript
export default defineConfig({
  esbuild: {
    target: 'es2022',
    // ...
  },
  // ...
});
bash
esbuild app.js --target=es2022

Basic example ​

Here’s a simple example using constructor injection to inject one service into another.

ts
import { 
injectable
,
inject
} from "@needle-di/core";
@
injectable
()
class
FooService
{
// ... } @
injectable
()
class
BarService
{
constructor(private
fooService
=
inject
(
FooService
)) {}
}

As you can see, Needle DI uses default parameter values for constructor injection.

The @injectable decorator eliminates the need to register services manually.

Bootstrapping ​

To bootstrap the BarService, you have to create a new dependency injection container, and use the .get() method on it to retrieve it by its token:

ts
import { 
injectable
,
inject
,
Container
} from "@needle-di/core";
import {
BarService
} from "./bar.service";
const
container
= new
Container
();
// you can use container.bind() to register more services. const
barService
=
container
.
get
(
BarService
);

That's it!

What's next? ​

Learn how you can use binding to register services.

Released under the MIT License