Override Node's require to programatically define "virtual" modules
Last updated 6 years ago by bluejeansandrain .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install vmodule 
SYNC missed versions from official npm registry.


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.


npm install vmodule --save


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".

Module "submodule.js"

var foo = require('foo');

// { the: 'exports' }


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.

Current Tags

  • 1.0.1                                ...           latest (6 years ago)

2 Versions

  • 1.0.1                                ...           6 years ago
  • 1.0.0                                ...           6 years ago
Maintainers (1)
Today 0
This Week 0
This Month 2
Last Day 0
Last Week 1
Last Month 0
Dependencies (5)
Dev Dependencies (2)
Dependents (0)

Copyright 2014 - 2016 © taobao.org |