Browse Source

Close #1281 Make require a public member of module

Reviewed by @felixge
v0.7.4-release
isaacs 14 years ago
parent
commit
9b5098f509
  1. 11
      doc/api/modules.markdown
  2. 7
      lib/module.js
  3. 2
      test/fixtures/module-require/child/index.js
  4. 1
      test/fixtures/module-require/child/node_modules/target.js
  5. 5
      test/fixtures/module-require/parent/index.js
  6. 1
      test/fixtures/module-require/parent/node_modules/target.js
  7. 7
      test/simple/test-module-loading.js

11
doc/api/modules.markdown

@ -172,6 +172,17 @@ y.js:
console.log(x.a); 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... ### All Together...
To get the exact filename that will be loaded when `require()` is called, use To get the exact filename that will be loaded when `require()` is called, use

7
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 // Returns exception if any
Module.prototype._compile = function(content, filename) { Module.prototype._compile = function(content, filename) {
var self = this; var self = this;
@ -343,7 +348,7 @@ Module.prototype._compile = function(content, filename) {
content = content.replace(/^\#\!.*/, ''); content = content.replace(/^\#\!.*/, '');
function require(path) { function require(path) {
return Module._load(path, self); return self.require(path);
} }
require.resolve = function(request) { require.resolve = function(request) {

2
test/fixtures/module-require/child/index.js

@ -0,0 +1,2 @@
exports.loaded = require('target');
exports.module = module;

1
test/fixtures/module-require/child/node_modules/target.js

@ -0,0 +1 @@
exports.loaded = 'from child';

5
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');

1
test/fixtures/module-require/parent/node_modules/target.js

@ -0,0 +1 @@
exports.loaded = 'from parent';

7
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'); 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() { process.addListener('exit', function() {
assert.ok(common.indirectInstanceOf(a.A, Function)); assert.ok(common.indirectInstanceOf(a.A, Function));
assert.equal('A done', a.A()); assert.equal('A done', a.A());

Loading…
Cancel
Save