function resolveSourceDir(rootDir, sourceDir) { var srcDir = path.resolve(rootDir, sourceDir); if (fs.existsSync(srcDir)) return srcDir; // Handle module-relative path, e.g. `loopback/common/models` var start = sourceDir.substring(0, 2); if (start !== './' && start !== '..') { // Module.globalPaths is a list of globally configured paths like // [ env.NODE_PATH values, $HOME/.node_modules, etc. ] // Module._nodeModulePaths(rootDir) returns a list of paths like // [ rootDir/node_modules, rootDir/../node_modules, etc. ] var modulePaths = Module.globalPaths .concat(Module._nodeModulePaths(rootDir)); srcDir = modulePaths .map(function(candidateDir) { return path.join(candidateDir, sourceDir); }) .filter(function(candidate) { return fs.existsSync(candidate); }) [0]; if (srcDir) return srcDir; } return undefined; }
function isInNodePath(resolvedPath) { if (!resolvedPath) return false; return Module.globalPaths .map((nodePath) => { return resolve(process.cwd(), nodePath) + pathsep; }) .some((fullNodePath) => { return resolvedPath.indexOf(fullNodePath) === 0; }); }
module.exports = function resolve(dirname) { // Check for environmental letiable if (process.env.APP_ROOT_PATH) { return path.resolve(process.env.APP_ROOT_PATH); } // Defer to main process in electron if ('type' in process && 'renderer' === process.type) { let remote = require('electron').remote; return remote.require('./').path; } let resolved = path.resolve(dirname); let alternateMethod = false; let appRootPath = null; // Make sure that we're not loaded from a global include path // Eg. $HOME/.node_modules // $HOME/.node_libraries // $PREFIX/lib/node globalPaths.forEach(function(globalPath) { if (!alternateMethod && 0 === resolved.indexOf(globalPath)) { alternateMethod = true; } }); // If the app-root-path library isn't loaded globally, // and node_modules exists in the path, just split __dirname let nodeModulesDir = sep + 'node_modules'; if (!alternateMethod && -1 !== resolved.indexOf(nodeModulesDir)) { let parts = resolved.split(nodeModulesDir); if (parts.length) { appRootPath = parts[0]; parts = null; } } // If the above didn't work, or this module is loaded globally, then // resort to require.main.filename (See http://nodejs.org/api/modules.html) if (alternateMethod || null == appRootPath) { appRootPath = path.dirname(require.main.filename); } // Handle global bin/ directory edge-case if (alternateMethod && -1 !== appRootPath.indexOf(npmGlobalModuleDir) && (appRootPath.length - 4) === appRootPath.indexOf(sep + 'bin')) { appRootPath = appRootPath.slice(0, -4); } // Return return appRootPath; };
function tryResolveAppPath(rootDir, relativePath, resolveOptions) { var fullPath; var start = relativePath.substring(0, 2); /* In order to retain backward compatibility, we need to support * two ways how to treat values that are not relative nor absolute * path (e.g. `relativePath = 'foobar'`) * - `resolveOptions.strict = true` searches in `node_modules` only * - `resolveOptions.strict = false` attempts to resolve the value * as a relative path first before searching `node_modules` */ resolveOptions = resolveOptions || { strict: true }; var isModuleRelative = false; if (relativePath[0] === '/') { fullPath = relativePath; } else if (start === './' || start === '..') { fullPath = path.resolve(rootDir, relativePath); } else if (!resolveOptions.strict) { isModuleRelative = true; fullPath = path.resolve(rootDir, relativePath); } if (fullPath) { // This check is needed to support paths pointing to a directory if (fileExistsSync(fullPath)) { return fullPath; } try { fullPath = require.resolve(fullPath); return fullPath; } catch (err) { if (!isModuleRelative) { debug ('Skipping %s - %s', fullPath, err); return undefined; } } } // Handle module-relative path, e.g. `loopback/common/models` // Module.globalPaths is a list of globally configured paths like // [ env.NODE_PATH values, $HOME/.node_modules, etc. ] // Module._nodeModulePaths(rootDir) returns a list of paths like // [ rootDir/node_modules, rootDir/../node_modules, etc. ] var modulePaths = Module.globalPaths .concat(Module._nodeModulePaths(rootDir)); fullPath = modulePaths .map(function(candidateDir) { var absPath = path.join(candidateDir, relativePath); try { // NOTE(bajtos) We need to create a proper String object here, // otherwise we can't attach additional properties to it /*jshint -W053 */ var filePath = new String(require.resolve(absPath)); filePath.unresolvedPath = absPath; return filePath; } catch (err) { return absPath; } }) .filter(function(candidate) { return fileExistsSync(candidate.toString()); }) [0]; if (fullPath) { if (fullPath.unresolvedPath && resolveOptions.fullResolve === false) return fullPath.unresolvedPath; // Convert String object back to plain string primitive return fullPath.toString(); } debug ('Skipping %s - module not found', fullPath); return undefined; }
'use strict'; var common = require('../common'); var assert = require('assert'); var module = require('module'); var partA, partB; var partC = ''; if (common.isWindows) { partA = 'C:\\Users\\Rocko Artischocko\\AppData\\Roaming\\npm'; partB = 'C:\\Program Files (x86)\\nodejs\\'; process.env['NODE_PATH'] = partA + ';' + partB + ';' + partC; } else { partA = '/usr/test/lib/node_modules'; partB = '/usr/test/lib/node'; process.env['NODE_PATH'] = partA + ':' + partB + ':' + partC; } module._initPaths(); assert.ok(module.globalPaths.indexOf(partA) !== -1); assert.ok(module.globalPaths.indexOf(partB) !== -1); assert.ok(module.globalPaths.indexOf(partC) === -1); assert.ok(Array.isArray(module.globalPaths));
const timers = require('timers'); const Module = require('module'); process.atomBinding = function(name) { try { return process.binding("atom_" + process.type + "_" + name); } catch (error) { if (/No such module/.test(error.message)) { return process.binding("atom_common_" + name); } } }; if (!process.env.ELECTRON_HIDE_INTERNAL_MODULES) { // Add common/api/lib to module search paths. Module.globalPaths.push(path.join(__dirname, 'api')); } // setImmediate and process.nextTick makes use of uv_check and uv_prepare to // run the callbacks, however since we only run uv loop on requests, the // callbacks wouldn't be called until something else activated the uv loop, // which would delay the callbacks for arbitrary long time. So we should // initiatively activate the uv loop once setImmediate and process.nextTick is // called. var wrapWithActivateUvLoop = function(func) { return function() { process.activateUvLoop(); return func.apply(this, arguments); }; };
var paths = require('module').globalPaths; if (Array.isArray(paths)) { var found = false; paths.forEach(function(elem) { if (elem === process.env.NODE_PATH) { found = true; } }); if (!found) process.exit(1); else setInterval(function keepAlive() {}, 10000); } else { process.exit(1); }
function tryResolveAppPath(rootDir, relativePath, resolveOptions) { var fullPath; var start = relativePath.substring(0, 2); /* In order to retain backward compatibility, we need to support * two ways how to treat values that are not relative nor absolute * path (e.g. `relativePath = 'foobar'`) * - `resolveOptions.strict = true` searches in `node_modules` only * - `resolveOptions.strict = false` attempts to resolve the value * as a relative path first before searching `node_modules` */ resolveOptions = resolveOptions || { strict: true }; var isModuleRelative = false; if (relativePath[0] === '/') { fullPath = relativePath; } else if (start === './' || start === '..') { fullPath = path.resolve(rootDir, relativePath); } else if (!resolveOptions.strict) { isModuleRelative = true; fullPath = path.resolve(rootDir, relativePath); } if (fullPath) { // This check is needed to support paths pointing to a directory if (fs.existsSync(fullPath)) { return fullPath; } try { fullPath = require.resolve(fullPath); return fullPath; } catch (err) { if (!isModuleRelative) { debug ('Skipping %s - %s', fullPath, err); return undefined; } } } // Handle module-relative path, e.g. `loopback/common/models` // Module.globalPaths is a list of globally configured paths like // [ env.NODE_PATH values, $HOME/.node_modules, etc. ] // Module._nodeModulePaths(rootDir) returns a list of paths like // [ rootDir/node_modules, rootDir/../node_modules, etc. ] var modulePaths = Module.globalPaths .concat(Module._nodeModulePaths(rootDir)); fullPath = modulePaths .map(function(candidateDir) { try { var filePath = path.join(candidateDir, relativePath); filePath = require.resolve(filePath); return filePath; } catch (err) { return filePath; } }) .filter(function(candidate) { return fs.existsSync(candidate); }) [0]; if (fullPath) return fullPath; debug ('Skipping %s - module not found', fullPath); return undefined; }
//------------------------------------------------------------------------------ // Private //------------------------------------------------------------------------------ const DEFAULT_OPTIONS = { /* * module.paths is an array of paths to search for resolving things relative * to this file. Module.globalPaths contains all of the special Node.js * directories that can also be searched for modules. * * Need to check for existence of module.paths because Jest seems not to * include it. See https://github.com/eslint/eslint/issues/5791. */ lookupPaths: module.paths ? module.paths.concat(Module.globalPaths) : Module.globalPaths.concat() }; /** * Resolves modules based on a set of options. * @param {Object} options The options for resolving modules. * @param {string[]} options.lookupPaths An array of paths to include in the * lookup with the highest priority paths coming first. * @constructor */ function ModuleResolver(options) { options = options || {}; this.options = lodash.assign({}, DEFAULT_OPTIONS, options); }
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE // USE OR OTHER DEALINGS IN THE SOFTWARE. 'use strict'; const common = require('../common'); const assert = require('assert'); const mod = require('module'); let partA, partB; const partC = ''; if (common.isWindows) { partA = 'C:\\Users\\Rocko Artischocko\\AppData\\Roaming\\npm'; partB = 'C:\\Program Files (x86)\\nodejs\\'; process.env['NODE_PATH'] = `${partA};${partB};${partC}`; } else { partA = '/usr/test/lib/node_modules'; partB = '/usr/test/lib/node'; process.env['NODE_PATH'] = `${partA}:${partB}:${partC}`; } mod._initPaths(); assert.ok(mod.globalPaths.includes(partA)); assert.ok(mod.globalPaths.includes(partB)); assert.ok(!mod.globalPaths.includes(partC)); assert.ok(Array.isArray(mod.globalPaths));