UNPKG

777 BJavaScriptView Raw
1'use strict';
2
3const configurationError = require('./configurationError');
4const globalModules = require('global-modules');
5const resolveFrom = require('resolve-from');
6
7/**
8 * @param {string} basedir
9 * @param {string} lookup
10 * @return {string}
11 */
12module.exports = function getModulePath(basedir, lookup) {
13 // 1. Try to resolve from the provided directory
14 // 2. Try to resolve from `process.cwd`
15 // 3. Try to resolve from global `node_modules` directory
16 let path = resolveFrom.silent(basedir, lookup);
17
18 if (!path) {
19 path = resolveFrom.silent(process.cwd(), lookup);
20 }
21
22 if (!path) {
23 path = resolveFrom.silent(globalModules, lookup);
24 }
25
26 if (!path) {
27 throw configurationError(`Could not find "${lookup}". Do you need a \`configBasedir\`?`);
28 }
29
30 return path;
31};