diff --git a/doc/api/modules.markdown b/doc/api/modules.markdown index d147395f038..9dfa1c486ab 100644 --- a/doc/api/modules.markdown +++ b/doc/api/modules.markdown @@ -172,6 +172,17 @@ y.js: console.log(x.a); +### module.require + +The `module.require` method provides a way to load a module as if +`require()` was called from the original module. + +Note that in order to do this, you must get a reference to the `module` +object. Since `require()` returns the `exports`, and the `module` is +typically *only* available within a specific module's code, it must be +explicitly exported in order to be used. + + ### All Together... To get the exact filename that will be loaded when `require()` is called, use diff --git a/lib/module.js b/lib/module.js index daf73d96f50..a0476ca9cd0 100644 --- a/lib/module.js +++ b/lib/module.js @@ -336,6 +336,11 @@ Module.prototype.load = function(filename) { }; +Module.prototype.require = function(path) { + return Module._load(path, this); +}; + + // Returns exception if any Module.prototype._compile = function(content, filename) { var self = this; @@ -343,7 +348,7 @@ Module.prototype._compile = function(content, filename) { content = content.replace(/^\#\!.*/, ''); function require(path) { - return Module._load(path, self); + return self.require(path); } require.resolve = function(request) { diff --git a/test/fixtures/module-require/child/index.js b/test/fixtures/module-require/child/index.js new file mode 100644 index 00000000000..6c2fd4ecba9 --- /dev/null +++ b/test/fixtures/module-require/child/index.js @@ -0,0 +1,2 @@ +exports.loaded = require('target'); +exports.module = module; diff --git a/test/fixtures/module-require/child/node_modules/target.js b/test/fixtures/module-require/child/node_modules/target.js new file mode 100644 index 00000000000..5df2baeb7e4 --- /dev/null +++ b/test/fixtures/module-require/child/node_modules/target.js @@ -0,0 +1 @@ +exports.loaded = 'from child'; diff --git a/test/fixtures/module-require/parent/index.js b/test/fixtures/module-require/parent/index.js new file mode 100644 index 00000000000..e174cba6fff --- /dev/null +++ b/test/fixtures/module-require/parent/index.js @@ -0,0 +1,5 @@ +var child = require('../child'); +//console.log(child.module.require, child.module); +console.log(child.module.require('target')); +console.log(child.loaded); +exports.loaded = child.module.require('target'); diff --git a/test/fixtures/module-require/parent/node_modules/target.js b/test/fixtures/module-require/parent/node_modules/target.js new file mode 100644 index 00000000000..ee7e1f19332 --- /dev/null +++ b/test/fixtures/module-require/parent/node_modules/target.js @@ -0,0 +1 @@ +exports.loaded = 'from parent'; diff --git a/test/simple/test-module-loading.js b/test/simple/test-module-loading.js index 421fcbf8446..53b1ad4494e 100644 --- a/test/simple/test-module-loading.js +++ b/test/simple/test-module-loading.js @@ -207,6 +207,13 @@ var amdExtraArgs = require(amdFolder + '/extra-args.js'); assert.equal(amdExtraArgs.ok, amdreg.ok, 'amd extra args failed'); +// make sure that module.require() is the same as +// doing require() inside of that module. +var parent = require('../fixtures/module-require/parent/'); +var child = require('../fixtures/module-require/child/'); +assert.equal(child.loaded, parent.loaded); + + process.addListener('exit', function() { assert.ok(common.indirectInstanceOf(a.A, Function)); assert.equal('A done', a.A());