Namespace support for node.js, making it easy to require an entire directory.
Want to see pretty graphs? Log in now!
npm install require-namespace
|1||downloads in the last day|
|3||downloads in the last week|
|47||downloads in the last month|
|Last Published By|
|Version||0.1.1 last updated a year ago|
|Dependencies||winston, async, underscore|
When you require modules in node.js you include the path to the file which means that moving files break dependent modules.
To avoid that issue the new notion of a namespace allows you to require a dependency in a way that is more loosely coupled to the directory structure on disk.
$ npm install require-namespace
During initialisation you synchronously create a namespace and associate it with a directory:
global.domain = namespace.createSync('domain', __dirname + '/domain/')
At this point the directory is recursively scanned and a record of each file is kept. Since we saved the namespace to a global variable we can then require files from it like this:
var LinkContentProcessor = domain.require('LinkContentProcessor')
That require will work if there was a file called 'LinkContentProcessor' anywhere within the directory we used when creating the namespace.
If we don't want to use a global variable for the namespace then requiring becomes a bit more wordy:
var domain = require('require-namespace')('domain') var LinkContentProcessor = domain.require('LinkContentProcessor')
The project comes with an example that you can run using
The tests use vows and can be run using:
- More tests
- More examples