
Product
Announcing Bun and vlt Support in Socket
Bringing supply chain security to the next generation of JavaScript package managers
di.libx.js
Advanced tools
> Lightweight & non intrusive Dependency Injection module that supports async/deferred resolution and uglified support for Typescript and JavaScript in 3.3kB gzipped (14.7kB on disk). Feature complete, fast, reliable and well tested. Compatible with Node.
Lightweight & non intrusive Dependency Injection module that supports async/deferred resolution and uglified support for Typescript and JavaScript in 3.3kB gzipped (14.7kB on disk). Feature complete, fast, reliable and well tested. Compatible with Node.js, Bun, and browsers.
Install via yarn (recommended):
yarn add di.libx.js
Install via npm:
npm install --save di.libx.js
https://cdn.jsdelivr.net/npm/di.libx.js@latest/dist/browser.min.js
(Modules are loaded into `window.libx.di` object).
Use this library in browser from CDN, code example, live example.
Or include from node_modules.
Basic usage:
import DependencyInjector from 'di.libx.js';
// const DependencyInjector = require('di.libx.js');
const di = new DependencyInjector();
const myFunc = () => {
console.log('This is myFunc');
};
// Register a dependencies
di.register('func', myFunc);
// Require dependencies. Will wait until all dependencies are ready
// Note that awaiting this function will create dead lock unless the other register will called in parallel
di.inject((func, anonFunc) => {
func();
anonFunc();
});
// Register another dependencies. Will trigger execution of the `require`
di.register('anonFunc', () => console.log('Anonymous func'));
Advanced usage:
// Using proxy access for automatic async resolution
const di = new DependencyInjector();
// Register a module
di.register('myService', {
async getData() {
return 'data';
}
});
// Access through proxy - automatically handles async resolution
const result = await di.proxy.myService.getData();
console.log(result); // 'data'
// Register async module
await di.registerAsync('asyncModule', Promise.resolve({
value: 'async value'
}));
// Register with no override protection
await di.register('protectedModule', 'value', true); // throws if module exists
// Inject and register a new module
await di.injectAndRegister('newModule', (existingModule) => {
return {
value: existingModule.value + ' enhanced'
};
});
More examples:
// in your web app add:
// <script src="https://cdn.jsdelivr.net/npm/di.libx.js@latest/dist/browser.min.js"></script>
var myModule = { somveVar: 1 };
libx.di.register('myModule', myModule);
// async require:
await libx.di.inject((myModule) => {
console.log('dependency resolved!', myModule);
// execute your code here. 'await' is optional incase you want it to be async and continue execution.
// note: the callback will be triggered only once the dependency is registered somewhere else in your program. Beware not to create dead-lock.
});
// synchronously get a module:
mod = await libx.di.require('myModule');
// register new module with other dependencies:
mod = libx.di.injectAndRegister('myNewModule', (myModule) => {
return () => console.log('this came from myNewModule!', myModule);
});
// inject for uglified code (second param is module identifiers, injected by position. So `myUglifiedModule` == `myModule`):
libx.di.inject(
(myUglifiedModule) => {
console.log('unglified dependency resolved!', myUglifiedModule);
},
['myModule']
);
// Register a local scoped container that inherits from the main container.
// All locally registered modules will be disposed once exited scope.
const subContainer = new DependencyInjector(di);
subContainer.register('moduleB', di.initiate(ModuleB));
// Main execution point:
subContainer.inject((moduleB) => {
const result = moduleB.Run(10);
console.log('Result: ', result);
}).then(() => {
console.log('DONE!');
});
Check more examples in unit-test at tests.
$ yarn build
$ yarn watch
$ yarn test
FAQs
> Lightweight & non intrusive Dependency Injection module that supports async/deferred resolution and uglified support for Typescript and JavaScript in 3.3kB gzipped (14.7kB on disk). Feature complete, fast, reliable and well tested. Compatible with Node.
We found that di.libx.js demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

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.

Product
Bringing supply chain security to the next generation of JavaScript package managers

Product
A safer, faster way to eliminate vulnerabilities without updating dependencies

Product
Reachability analysis for Ruby is now in beta, helping teams identify which vulnerabilities are truly exploitable in their applications.