Skip to content

Commit 6cdeb3b

Browse files
isaacsry
authored andcommitted
A module ID with a trailing slash must be a dir.
require('./foo/') should not try to load './foo.js'. It should only look for ./foo/index.js Closes nodejsGH-588
1 parent 36ef564 commit 6cdeb3b

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

lib/module.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ Module._findPath = function(request, paths) {
5858
paths = [''];
5959
}
6060

61+
var trailingSlash = (request.slice(-1) === '/');
62+
6163
// check if the file exists and is not a directory
6264
function tryFile(requestPath) {
6365
try {
@@ -89,13 +91,16 @@ Module._findPath = function(request, paths) {
8991
// For each path
9092
for (var i = 0, PL = paths.length; i < PL; i++) {
9193
var basePath = path.resolve(paths[i], request);
94+
var filename;
9295

93-
// try to join the request to the path
94-
var filename = tryFile(basePath);
96+
if (!trailingSlash) {
97+
// try to join the request to the path
98+
filename = tryFile(basePath);
9599

96-
if (!filename) {
97-
// try it with each of the extensions
98-
filename = tryExtensions(basePath);
100+
if (!filename && !trailingSlash) {
101+
// try it with each of the extensions
102+
filename = tryExtensions(basePath);
103+
}
99104
}
100105

101106
if (!filename) {

test/fixtures/nested-index/three.js

Whitespace-only changes.

test/fixtures/nested-index/three/index.js

Whitespace-only changes.

test/simple/test-module-loading.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ var one = require('../fixtures/nested-index/one'),
5252
two = require('../fixtures/nested-index/two');
5353
assert.notEqual(one.hello, two.hello);
5454

55+
common.debug('test index.js in a folder with a trailing slash');
56+
var three = require('../fixtures/nested-index/three'),
57+
threeFolder = require('../fixtures/nested-index/three/'),
58+
threeIndex = require('../fixtures/nested-index/three/index.js');
59+
assert.equal(threeFolder, threeIndex);
60+
assert.notEqual(threeFolder, three);
61+
5562
common.debug('test cycles containing a .. path');
5663
var root = require('../fixtures/cycles/root'),
5764
foo = require('../fixtures/cycles/folder/foo');

0 commit comments

Comments
 (0)