diff --git a/doc/api.txt b/doc/api.txt index 74a73b9db5..ca1c6432da 100644 --- a/doc/api.txt +++ b/doc/api.txt @@ -64,6 +64,9 @@ The search path for absolute path arguments to +require()+. +__filename+ :: The filename of the script being executed. ++__dirname+ :: +The dirname of the script being executed. + +module+ :: A reference to the current module (of type +process.Module+). In particular +module.exports+ is the same as the +exports+ object. See +src/process.js+ for diff --git a/src/node.js b/src/node.js index b905f17430..8cbb7312c8 100644 --- a/src/node.js +++ b/src/node.js @@ -919,13 +919,13 @@ Module.prototype.loadScript = function (filename, loadPromise) { require.async = requireAsync; require.main = process.mainModule; // create wrapper function - var wrapper = "var __wrap__ = function (exports, require, module, __filename) { " + var wrapper = "var __wrap__ = function (exports, require, module, __filename, __dirname) { " + content + "\n}; __wrap__;"; try { var compiledWrapper = process.compile(wrapper, filename); - compiledWrapper.apply(self.exports, [self.exports, require, self, filename]); + compiledWrapper.apply(self.exports, [self.exports, require, self, filename, path.dirname(filename)]); } catch (e) { loadPromise.emitError(e); return; diff --git a/test/mjsunit/test-module-loading.js b/test/mjsunit/test-module-loading.js index 09b58074f9..2ff9c41b41 100644 --- a/test/mjsunit/test-module-loading.js +++ b/test/mjsunit/test-module-loading.js @@ -6,7 +6,7 @@ var a = require("./fixtures/a"); var d = require("./fixtures/b/d"); var d2 = require("./fixtures/b/d"); // Absolute -var d3 = require(require('path').dirname(__filename)+"/fixtures/b/d"); +var d3 = require(__dirname+"/fixtures/b/d"); // Relative var d4 = require("../mjsunit/fixtures/b/d"); @@ -52,6 +52,8 @@ try { assert.equal("blah", e.message); } +assert.equal(require('path').dirname(__filename), __dirname); + process.addListener("exit", function () { assert.equal(true, a.A instanceof Function); assert.equal("A done", a.A());