1 var path = require('path');
2 var parse = path.parse || require('path-parse');
4 module.exports = function nodeModulesPaths(start, opts) {
5 var modules = opts && opts.moduleDirectory
6 ? [].concat(opts.moduleDirectory)
10 // ensure that `start` is an absolute path at this point,
11 // resolving against the process' current working directory
12 start = path.resolve(start);
15 if (/^([A-Za-z]:)/.test(start)) {
17 } else if (/^\\\\/.test(start)) {
22 var parsed = parse(start);
23 while (parsed.dir !== paths[paths.length - 1]) {
24 paths.push(parsed.dir);
25 parsed = parse(parsed.dir);
28 var dirs = paths.reduce(function (dirs, aPath) {
29 return dirs.concat(modules.map(function (moduleDir) {
30 return path.join(prefix, aPath, moduleDir);
34 return opts && opts.paths ? dirs.concat(opts.paths) : dirs;