npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

dibuilder

v1.1.6

Published

Module to easily inject dependencies in your nodeJS api

Downloads

43

Readme

DIBuilder - Dependency Injection Builder

Module to easily inject dependencies in your nodeJS api.

With this module, you need only to make your classes receive the dependencies as parameters and let the DIBuilder build it all.

You don't even need to require all your modules in the server.js. Just config the folders where you want it to search for modules, and let DIBuiler do it for you.

You can add instances you already have too in server.js, like routes or mongoose.

If any of your modules depends on any module from node or node_modules and you don't require it in your server.js, don't worry, the plugin will require them for you when some module depends on it.

Just use the module name as dependency name, replacing any hyphen with the next letter uppercase. (ex.: if the module is aws-sdk you should depend on awsSdk).

DIBuilder will warn you about errors like circular dependencies, error in constructor and dependence on modules that returns nothing, providing valuable information to discover where's the error in your code.

Set the DEBUG env var to see debug info. In windows cmd: set DEBUG=DIBuilder

Run the project inside example folder to see it working.

Getting started:

Install plugin with npm:

npm install dibuilder --save

in your server.js file, require dibuilder module:

	var dibuilder = require('dibuilder');

Add the instances you already have in server.js and that your modules can depend on, like router and mongoose:

	dibuilder.addInstance('router', router);
	dibuilder.addInstance('mongoose', mongoose);

Add the paths to the folders where DIBuilder should search for modules and automatically require them:

	dibuilder.loadModules(path_module.join(__dirname, './project/resources'));
	dibuilder.loadModules(path_module.join(__dirname, './project/domain'));

Make your modules receive a dibuilder instance and use it to add that module:

	module.exports = function(dibuilder) {
		//add this module
	    dibuilder.addModule(itemResource);
	    
	    function itemResource (router, itemService, fs) { //plugin will inject router, itemService and fs into it
	        // definition
	        router.get('/item', listResource);

	        // implementation
			function listResource(){
			}
	    }
	};

If instead, you want to add any modules manually in server.js, just use the addModule passing your function

 dibuilder.addModule(itemResource);

And finally, call the build function to inject the dependencies. You can pass a success callback, where you could set the route, connect to your database, start the server etc..

dibuilder.build(function(){
    router.get('/', function(req, res){
        res.send("DIBuilder example!");
    });

    //register api routes
    app.use('/api', router);

    mongoose.connect('mongodb://localhost/example');

    server.listen(appPort, function() {
        console.log('Express server listening on port ' + appPort);
    });     
});