Browse Source

Closes GH-619 Make require.main be the main module

v0.7.4-release
isaacs 14 years ago
committed by Ryan Dahl
parent
commit
f86ec1366f
  1. 11
      lib/module.js
  2. 4
      test/fixtures/not-main-module.js
  3. 8
      test/simple/test-module-loading.js

11
lib/module.js

@ -155,7 +155,7 @@ Module._resolveLookupPaths = function(request, parent) {
}; };
Module._load = function(request, parent) { Module._load = function(request, parent, isMain) {
if (parent) { if (parent) {
debug('Module._load REQUEST ' + (request) + ' parent: ' + parent.id); debug('Module._load REQUEST ' + (request) + ' parent: ' + parent.id);
} }
@ -183,6 +183,12 @@ Module._load = function(request, parent) {
} }
var module = new Module(id, parent); var module = new Module(id, parent);
if (isMain) {
process.mainModule = module;
module.id = '.';
}
Module._cache[filename] = module; Module._cache[filename] = module;
module.load(filename); module.load(filename);
return module.exports; return module.exports;
@ -306,8 +312,7 @@ Module._extensions['.node'] = function(module, filename) {
// bootstrap main module. // bootstrap main module.
Module.runMain = function() { Module.runMain = function() {
// Load the main module--the command line argument. // Load the main module--the command line argument.
process.mainModule = new Module('.'); Module._load(process.argv[1], null, true);
Module._load(process.argv[1]);
}; };
Module._initPaths = function() { Module._initPaths = function() {

4
test/fixtures/not-main-module.js

@ -0,0 +1,4 @@
var assert = require('assert');
assert.notEqual(module, require.main, 'require.main should not == module');
assert.notEqual(module, process.mainModule,
'process.mainModule should not === module');

8
test/simple/test-module-loading.js

@ -5,6 +5,14 @@ var fs = require('fs');
common.debug('load test-module-loading.js'); common.debug('load test-module-loading.js');
// assert that this is the main module.
assert.equal(require.main.id, '.', 'main module should have id of \'.\'');
assert.equal(require.main, module, 'require.main should === module');
assert.equal(process.mainModule, module,
'process.mainModule should === module');
// assert that it's *not* the main module in the required module.
require('../fixtures/not-main-module.js');
// require a file with a request that includes the extension // require a file with a request that includes the extension
var a_js = require('../fixtures/a.js'); var a_js = require('../fixtures/a.js');
assert.equal(42, a_js.number); assert.equal(42, a_js.number);

Loading…
Cancel
Save