diff --git a/lib/repl.js b/lib/repl.js index d0400235e7..91a38d49e5 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -31,6 +31,8 @@ function cwdRequire (id) { Object.keys(require).forEach(function (k) { cwdRequire[k] = require[k]; }); +// hack for require.resolve("./relative") to work properly. +module.filename = process.cwd() + "/repl"; function resetContext() { context = Script.createContext(); diff --git a/src/node.js b/src/node.js index 094219b5a2..afe07cdbe6 100644 --- a/src/node.js +++ b/src/node.js @@ -164,10 +164,8 @@ var module = (function () { }; } - function findModulePath (id, dirs) { - process.assert(Array.isArray(dirs)); - - var nextLoc = traverser(id, id.charAt(0) === '/' ? [''] : dirs); + function findModulePath (request, paths) { + var nextLoc = traverser(request, request.charAt(0) === '/' ? [''] : paths); var fs = requireNative('fs'); @@ -181,7 +179,10 @@ var module = (function () { // sync - no i/o performed - function resolveModulePath(request, parent) { + function resolveModuleLookupPaths (request, parent) { + + if (natives[request]) return [request, []]; + var start = request.substring(0, 2); if (start !== "./" && start !== "..") { return [request, modulePaths]; @@ -207,18 +208,33 @@ var module = (function () { function loadModule (request, parent) { debug("loadModule REQUEST " + (request) + " parent: " + parent.id); + var resolved = resolveModuleFilename(request, parent); + var id = resolved[0]; + var filename = resolved[1]; + // With natives id === request // We deal with these first - var cachedNative = internalModuleCache[request]; + var cachedNative = internalModuleCache[id]; if (cachedNative) { return cachedNative.exports; } - if (natives[request]) { + if (natives[id]) { debug('load native module ' + request); - return loadNative(request).exports; + return loadNative(id).exports; } - var resolvedModule = resolveModulePath(request, parent), + var cachedModule = moduleCache[filename]; + if (cachedModule) return cachedModule.exports; + + var module = new Module(id, parent); + moduleCache[filename] = module; + module.load(filename); + return module.exports; + }; + + function resolveModuleFilename (request, parent) { + if (natives[request]) return [request, request]; + var resolvedModule = resolveModuleLookupPaths(request, parent), id = resolvedModule[0], paths = resolvedModule[1]; @@ -228,15 +244,8 @@ var module = (function () { if (!filename) { throw new Error("Cannot find module '" + request + "'"); } - - var cachedModule = moduleCache[filename]; - if (cachedModule) return cachedModule.exports; - - var module = new Module(id, parent); - moduleCache[filename] = module; - module.load(filename); - return module.exports; - }; + return [id, filename]; + } Module.prototype.load = function (filename) { @@ -262,6 +271,9 @@ var module = (function () { return loadModule(path, self); } + require.resolve = function (request) { + return resolveModuleFilename(request, self)[1]; + } require.paths = modulePaths; require.main = process.mainModule; // Enable support to add extra extension types diff --git a/test/simple/test-require-resolve.js b/test/simple/test-require-resolve.js new file mode 100644 index 0000000000..37bb284606 --- /dev/null +++ b/test/simple/test-require-resolve.js @@ -0,0 +1,14 @@ +var common = require("../common"); +var fixturesDir = common.fixturesDir; +var assert = common.assert; +var path = require("path"); + +assert.equal(path.join(__dirname, "../fixtures/a.js"), + require.resolve("../fixtures/a")); +assert.equal(path.join(fixturesDir, "a.js"), + require.resolve(path.join(fixturesDir, "a"))); +assert.equal(path.join(fixturesDir, "nested-index", "one", "index.js"), + require.resolve("../fixtures/nested-index/one")); +assert.equal("path", require.resolve("path")); + +console.log("ok");