Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/MattiSG/requirewith
Dependency injection for Node’s require
https://github.com/MattiSG/requirewith
Last synced: about 11 hours ago
JSON representation
Dependency injection for Node’s require
- Host: GitHub
- URL: https://github.com/MattiSG/requirewith
- Owner: MattiSG
- Created: 2012-07-19T11:29:46.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2012-07-19T13:45:47.000Z (over 12 years ago)
- Last Synced: 2024-10-14T14:47:32.106Z (27 days ago)
- Language: JavaScript
- Size: 109 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
RequireWith
===========Allows you to do inject dependencies in (i.e. pass variables to) a `require`d Node.js module.
Usage
-----var requireWith = require('mattisg.requirewith');
var mod = requireWith('./mymodule', {
config: config,
client: someVar
});Then, the `config` and `client` variables will be available in `mymodule`.
Redefining `require`
--------------------You can even redefine `require` for a one-line replacement:
require = require('mattisg.requirewith');
This module calls `require` directly if no argument other than the module name is given, so it is perfectly safe to redefine `require`. Actually, unless you specify so explicitly, this will automatically be done recursively for modules required with injections.
To disable recursive redefinitions, pass `false` as the third argument. To summarize:
require = require('mattisg.requirewith');
var path = require('path'); // exactly the same as no redefinition
var mymod = require('./mymodule', { // config can be used in mymodule, and mymodule does not need to redefine require to get injection capabilities
config: config
});var norec = requireWith('./mymodule', { // config can be used in mymodule, but it gets only the default require
config: config
}, false);# Installation
npm install mattisg.requirewith