diff --git a/lib/module.js b/lib/module.js index b9067d24af..8738c4cdd6 100644 --- a/lib/module.js +++ b/lib/module.js @@ -179,7 +179,6 @@ function resolveModulePath(request, parent) { var id, paths; if (request.charAt(0) == "." && (request.charAt(1) == "/" || request.charAt(1) == ".")) { // Relative request - debug("RELATIVE: requested:" + request + " set ID to: "+id+" from "+parent.id); var exts = ['js', 'node'], ext; var extensions = Object.keys(extensionCache); @@ -191,6 +190,12 @@ function resolveModulePath(request, parent) { var parentIdPath = path.dirname(parent.id + (path.basename(parent.filename).match(new RegExp('^index\\.(' + exts.join('|') + ')$')) ? "/" : "")); id = path.join(parentIdPath, request); + // make sure require('./path') and require('path') get distinct ids, even + // when called from the toplevel js file + if (parentIdPath == '.' && ! id.match(new RegExp('/'))) { + id = './' + id; + } + debug("RELATIVE: requested:" + request + " set ID to: "+id+" from "+parent.id); paths = [path.dirname(parent.filename)]; } else { id = request; diff --git a/test/simple/path.js b/test/simple/path.js new file mode 100644 index 0000000000..fb8d40ff0a --- /dev/null +++ b/test/simple/path.js @@ -0,0 +1,5 @@ +// This is actually more a fixture than a test. It is used to make +// sure that require('./path') and require('path') do different things. +// It has to be in the same directory as the test 'test-module-loading.js' +// and it has to have the same name as an internal module. +exports.path_func = function() { return "path_func"} diff --git a/test/simple/test-module-loading.js b/test/simple/test-module-loading.js index 220c3d163c..78183ce445 100644 --- a/test/simple/test-module-loading.js +++ b/test/simple/test-module-loading.js @@ -48,6 +48,13 @@ var root = require("../fixtures/cycles/root"), assert.equal(root.foo, foo); assert.equal(root.sayHello(), root.hello); +debug("test name clashes"); +// this one exists and should import the local module +var my_path = require("./path"); +assert.equal(true, my_path.path_func instanceof Function); +// this one does not exist and should throw +assert.throws(function() { require("./utils")}); + var errorThrown = false; try { require("../fixtures/throws_error");