diff --git a/lib/module.js b/lib/module.js index ef0fad209ff9c8..a7f92ea8ef3ba4 100644 --- a/lib/module.js +++ b/lib/module.js @@ -11,7 +11,7 @@ const path = require('path'); const internalModuleReadFile = process.binding('fs').internalModuleReadFile; const internalModuleStat = process.binding('fs').internalModuleStat; - +var moduleCache = {}; // If obj.hasOwnProperty has been overridden, then calling // obj.hasOwnProperty(prop) will break. // See: https://github.com/joyent/node/issues/1707 @@ -76,7 +76,7 @@ function readPackage(requestPath) { } try { - var pkg = packageMainCache[requestPath] = JSON.parse(json).main; + var pkg = packageMainCache[requestPath] = JSON.parse(json); } catch (e) { e.path = jsonPath; e.message = 'Error parsing ' + jsonPath + ': ' + e.message; @@ -88,9 +88,18 @@ function readPackage(requestPath) { function tryPackage(requestPath, exts) { var pkg = readPackage(requestPath); - if (!pkg) return false; + if (!(pkg && pkg.main)) return false; + + var resolved = pkg['_resolved'], + main = pkg.main; + + var filename; + if(resolved) { + filename = moduleCache[resolved] || (moduleCache[resolved] = path.resolve(requestPath, main)); + }else{ + filename = path.resolve(requestPath, main); + } - var filename = path.resolve(requestPath, pkg); return tryFile(filename) || tryExtensions(filename, exts) || tryExtensions(path.resolve(filename, 'index'), exts); }