1.0.1 • Published 9 years ago

vmodule v1.0.1

Weekly downloads
2
License
MIT
Repository
github
Last release
9 years ago

vmodule

Define a virtual module. A virtual module is one that is not backed by a file.

Virtual modules are implemented by replacing the require function in all modules loaded after vmodule is called for the first time. Virtual modules will override real modules, even if the real module has already been required and cached by Node's module system.

Install

npm install vmodule --save

Usage

Module "index.js"

var vmodule = require('vmodule');

vmodule('foo', { the: 'exports' });

// Note that the current module's `require()` method, and all modules which have
// already been loaded, are not affected. All modules loaded AFTER the first
// call to `vmodule()` will be able to require "foo".
require("./submodule.js");

Module "submodule.js"

var foo = require('foo');

console.log(foo);
// { the: 'exports' }

API

vmodule(string id, any exports, [Object options])

id must be a string, and can be a relative path, absolute path, or simple name. If it's a relative path, it will be resolved to an absolute path using the path of the module that called vmodule() and any future required relative paths that resolve to the same absolute path will match.

exports can be anything but null or undefined.

options is optional. There is currently only one option: global. If the global option { global: true } is set, then calling require() in any module that is loaded after vmodule() is called, will resolve the id to the virtual module's exports, regardless of the defining and requiring module paths. Without the global option only calls to require() by modules in the defining module's directory or a subdirectory will be affected, and only if the subdirectory does not include a node_modules directory.