Browse Source

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 GH-588
v0.7.4-release
isaacs 14 years ago
committed by Ryan Dahl
parent
commit
6cdeb3b3fd
  1. 15
      lib/module.js
  2. 0
      test/fixtures/nested-index/three.js
  3. 0
      test/fixtures/nested-index/three/index.js
  4. 7
      test/simple/test-module-loading.js

15
lib/module.js

@ -58,6 +58,8 @@ Module._findPath = function(request, paths) {
paths = ['']; paths = [''];
} }
var trailingSlash = (request.slice(-1) === '/');
// check if the file exists and is not a directory // check if the file exists and is not a directory
function tryFile(requestPath) { function tryFile(requestPath) {
try { try {
@ -89,13 +91,16 @@ Module._findPath = function(request, paths) {
// For each path // For each path
for (var i = 0, PL = paths.length; i < PL; i++) { for (var i = 0, PL = paths.length; i < PL; i++) {
var basePath = path.resolve(paths[i], request); var basePath = path.resolve(paths[i], request);
var filename;
// try to join the request to the path if (!trailingSlash) {
var filename = tryFile(basePath); // try to join the request to the path
filename = tryFile(basePath);
if (!filename) { if (!filename && !trailingSlash) {
// try it with each of the extensions // try it with each of the extensions
filename = tryExtensions(basePath); filename = tryExtensions(basePath);
}
} }
if (!filename) { if (!filename) {

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

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

7
test/simple/test-module-loading.js

@ -52,6 +52,13 @@ var one = require('../fixtures/nested-index/one'),
two = require('../fixtures/nested-index/two'); two = require('../fixtures/nested-index/two');
assert.notEqual(one.hello, two.hello); assert.notEqual(one.hello, two.hello);
common.debug('test index.js in a folder with a trailing slash');
var three = require('../fixtures/nested-index/three'),
threeFolder = require('../fixtures/nested-index/three/'),
threeIndex = require('../fixtures/nested-index/three/index.js');
assert.equal(threeFolder, threeIndex);
assert.notEqual(threeFolder, three);
common.debug('test cycles containing a .. path'); common.debug('test cycles containing a .. path');
var root = require('../fixtures/cycles/root'), var root = require('../fixtures/cycles/root'),
foo = require('../fixtures/cycles/folder/foo'); foo = require('../fixtures/cycles/folder/foo');

Loading…
Cancel
Save