Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

awilix

Package Overview
Dependencies
Maintainers
1
Versions
94
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

awilix

Extremely powerful dependency injection container.

  • 12.0.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
186K
decreased by-4.81%
Maintainers
1
Weekly downloads
 
Created

What is awilix?

Awilix is a powerful and flexible dependency injection (DI) container for JavaScript and Node.js applications. It helps manage dependencies in a clean and modular way, making it easier to write maintainable and testable code.

What are awilix's main functionalities?

Container Creation

This feature allows you to create a DI container where you can register your dependencies. The `createContainer` function initializes a new container instance.

const { createContainer, asClass, asFunction, asValue } = require('awilix');
const container = createContainer();

Registering Dependencies

You can register different types of dependencies in the container using `asClass`, `asFunction`, and `asValue`. This example shows how to register a class, a function, and a value.

class MyService {}
function myFunction() {}
const myValue = 'some value';

container.register({
  myService: asClass(MyService),
  myFunction: asFunction(myFunction),
  myValue: asValue(myValue)
});

Resolving Dependencies

Once dependencies are registered, you can resolve them from the container. This example demonstrates how to resolve a class instance, a function, and a value.

const myServiceInstance = container.resolve('myService');
const myFunctionInstance = container.resolve('myFunction');
const myValueInstance = container.resolve('myValue');

Scoped and Singleton Registrations

Awilix supports different lifetimes for dependencies. You can register dependencies as scoped or singleton. Scoped dependencies are created per resolution scope, while singletons are created once and shared.

container.register({
  myScopedService: asClass(MyService).scoped(),
  mySingletonService: asClass(MyService).singleton()
});

Auto-Loading Modules

Awilix can automatically load and register modules from specified directories. This feature is useful for large applications where manual registration would be cumbersome.

const path = require('path');
container.loadModules([path.join(__dirname, 'services/*.js')], {
  formatName: 'camelCase',
  resolverOptions: {
    lifetime: 'SINGLETON'
  }
});

Other packages similar to awilix

Keywords

FAQs

Package last updated on 03 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc