Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Close #1281 Make require a public member of module
Browse files Browse the repository at this point in the history
Reviewed by @felixge
  • Loading branch information
isaacs committed Jul 14, 2011
1 parent 876712a commit 9b5098f
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 1 deletion.
11 changes: 11 additions & 0 deletions doc/api/modules.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,14 +336,19 @@ 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;
// remove shebang
content = content.replace(/^\#\!.*/, '');

function require(path) {
return Module._load(path, self);
return self.require(path);
}

require.resolve = function(request) {
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/module-require/child/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exports.loaded = require('target');
exports.module = module;
1 change: 1 addition & 0 deletions test/fixtures/module-require/child/node_modules/target.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/fixtures/module-require/parent/index.js
Original file line number Diff line number Diff line change
@@ -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');
1 change: 1 addition & 0 deletions test/fixtures/module-require/parent/node_modules/target.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions test/simple/test-module-loading.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 9b5098f

Please sign in to comment.