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

node-simple-dependency-injector

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-simple-dependency-injector

Simple dependency injector for node.js

  • 1.0.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

* 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

Keywords

FAQs

Package last updated on 22 Mar 2016

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