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 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-simple-dependency-injector

v1.0.6

Published

Simple dependency injector for node.js

Downloads

18

Readme

* I'm working on inproving this readme, in the meantime you can contact me if you have any questions!

node-simple-dependency-injector

This simple dependency injector for node is aimed to help solve a very anoying issue when developing in node.js.
Having to require modules by where they are, their physical location relative to where you are now, instead of simply specifying what you need

Installation

  npm install node-simple-dependency-injector --save

Usage

Add the following line at the entry point of your node application

require('node-simple-dependency-injector').config(__dirname);

This will add a global inject function to your application that we will use below.

inject will first try to match your some/module/name to an entry in your config file, if no match was found it will fallback to a normal require call with the same param.

Configuration

node-simple-dependency-injector will look for a file called injectorConfig.json in your __dirname that will hold your configuration.

Your configuration is a map between the logical module/package names to the physical path in your code base.

Another option is to pass the a json object as the second parameter to config

Example configuration file
{
    "base": "./server/lib",
    "modules": {
        "package1": {
            "module1": "./some-package/some-module",
            "module2": "./some-package/other-module"
        }
    }
}

With the above configuration you can require your modules like this:

var someModule = inject('package1/module1'); // same as requiring (../)*some-package/some-module

The cool thing is that you can have a different config file when you are running your tests and inject what ever you need

Example test configuration file
{
    "base": "./server/lib",
    "modules": {
        "package1": {
            "module1": "../test/some-package/some-module-mock"
        }
    }
}

Now when running the same line as before you will get some-module-mock in all places you injected package1/module1 instead of getting some-module like we did before.

Path building

We have 3 variables here that will construct the path to your module.

  1. root - The first parameter to the config function
  2. base - The value of base in the config json
  3. path - The value of module-logical-name in your config json

Basically all paths are resolved like this path.join(root, base, path).

  • root will usually be the __dirname in your entry point file
  • base will be the path from your entry point file to the root of your code base (just to avoid having to write it over and over again for each module)
  • path should now be the path between where base point and where the actual file is