1 var path = require('path');
2 var parse = path.parse || require('path-parse');
4 var getNodeModulesDirs = function getNodeModulesDirs(absoluteStart, modules) {
6 if ((/^([A-Za-z]:)/).test(absoluteStart)) {
8 } else if ((/^\\\\/).test(absoluteStart)) {
12 var paths = [absoluteStart];
13 var parsed = parse(absoluteStart);
14 while (parsed.dir !== paths[paths.length - 1]) {
15 paths.push(parsed.dir);
16 parsed = parse(parsed.dir);
19 return paths.reduce(function (dirs, aPath) {
20 return dirs.concat(modules.map(function (moduleDir) {
21 return path.resolve(prefix, aPath, moduleDir);
26 module.exports = function nodeModulesPaths(start, opts, request) {
27 var modules = opts && opts.moduleDirectory
28 ? [].concat(opts.moduleDirectory)
31 if (opts && typeof opts.paths === 'function') {
35 function () { return getNodeModulesDirs(start, modules); },
40 var dirs = getNodeModulesDirs(start, modules);
41 return opts && opts.paths ? dirs.concat(opts.paths) : dirs;