-
-
Save isaacs/508773 to your computer and use it in GitHub Desktop.
module.patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/lib/module.js b/lib/module.js | |
index 72814a7..0827d13 100644 | |
--- a/lib/module.js | |
+++ b/lib/module.js | |
@@ -83,17 +83,6 @@ var pathFn = process.compile("(function (exports) {" + natives.path + "\n})", | |
var pathModule = createInternalModule('path', pathFn); | |
var path = pathModule.exports; | |
-function existsSync (path) { | |
- try { | |
- process.binding('fs').stat(path); | |
- return true; | |
- } catch (e) { | |
- return false; | |
- } | |
-} | |
- | |
- | |
- | |
var modulePaths = [path.join(process.execPath, "..", "..", "lib", "node")]; | |
if (process.env["HOME"]) { | |
@@ -104,7 +93,6 @@ if (process.env["NODE_PATH"]) { | |
modulePaths = process.env["NODE_PATH"].split(":").concat(modulePaths); | |
} | |
- | |
/* Sync unless callback given */ | |
function findModulePath (id, dirs, callback) { | |
process.assert(dirs.constructor == Array); | |
@@ -154,6 +142,8 @@ function findModulePath (id, dirs, callback) { | |
locations.push(path.join(dir, id, 'index' + ext)); | |
} | |
+ var fs = requireNative('fs'); | |
+ | |
function searchLocations () { | |
var location = locations.shift(); | |
if (!location) { | |
@@ -162,22 +152,21 @@ function findModulePath (id, dirs, callback) { | |
// if async | |
if (callback) { | |
- path.exists(location, function (found) { | |
- if (found) { | |
- callback(location); | |
- } else { | |
- return searchLocations(); | |
- } | |
+ fs.stat(location, function (err, stats) { | |
+ if (stats && !stats.isDirectory()) callback(location); | |
}); | |
+ return; | |
// if sync | |
} else { | |
- if (existsSync(location)) { | |
- return location; | |
- } else { | |
- return searchLocations(); | |
- } | |
+ try { | |
+ var stats = fs.statSync(location); | |
+ if (stats && !stats.isDirectory()) return location; | |
+ } catch(e) {} | |
} | |
+ | |
+ // fallthrough: couldn't find it this time. | |
+ return searchLocations(); | |
} | |
return searchLocations(); | |
} | |
diff --git a/test/simple/test-module-loading.js b/test/simple/test-module-loading.js | |
index 5245e4e..df71137 100644 | |
--- a/test/simple/test-module-loading.js | |
+++ b/test/simple/test-module-loading.js | |
@@ -106,6 +106,13 @@ process.assert(foo.bar.expect === foo.bar.actual); | |
assert.equal(require('../fixtures/foo').foo, 'ok', | |
'require module with no extension'); | |
+// Should not attempt to load a directory | |
+try { | |
+ require("../fixtures/empty") | |
+} catch(e) { | |
+ assert.equal(e.message, "Cannot find module '../fixtures/empty'") | |
+} | |
+ | |
process.addListener("exit", function () { | |
assert.equal(true, a.A instanceof Function); | |
assert.equal("A done", a.A()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment