|  | 
|  | 1 | +'use strict'; | 
|  | 2 | +require('../common'); | 
|  | 3 | +const assert = require('assert'); | 
|  | 4 | +const path = require('path'); | 
|  | 5 | +const nodeModules = path.join(__dirname, 'node_modules'); | 
|  | 6 | +const nestedNodeModules = path.join(__dirname, 'node_modules', 'node_modules'); | 
|  | 7 | +const nestedIndex = path.join(__dirname, 'nested-index'); | 
|  | 8 | + | 
|  | 9 | +// Test the default behavior. | 
|  | 10 | +assert.strictEqual( | 
|  | 11 | +  require.resolve('bar'), | 
|  | 12 | +  path.join(nodeModules, 'bar.js') | 
|  | 13 | +); | 
|  | 14 | + | 
|  | 15 | +// Verify that existing paths are removed. | 
|  | 16 | +assert.throws(() => { | 
|  | 17 | +  require.resolve('bar', { paths: [] }) | 
|  | 18 | +}, /^Error: Cannot find module 'bar'$/); | 
|  | 19 | + | 
|  | 20 | +// Verify that resolution path can be overwritten. | 
|  | 21 | +{ | 
|  | 22 | +  // three.js cannot be loaded from this file by default. | 
|  | 23 | +  assert.throws(() => { | 
|  | 24 | +    require.resolve('three') | 
|  | 25 | +  }, /^Error: Cannot find module 'three'$/); | 
|  | 26 | + | 
|  | 27 | +  // However, it can be found if resolution contains the nested index directory. | 
|  | 28 | +  assert.strictEqual( | 
|  | 29 | +    require.resolve('three', { paths: [nestedIndex] }), | 
|  | 30 | +    path.join(nestedIndex, 'three.js') | 
|  | 31 | +  ); | 
|  | 32 | + | 
|  | 33 | +  // Resolution from nested index directory also checks node_modules. | 
|  | 34 | +  assert.strictEqual( | 
|  | 35 | +    require.resolve('bar', { paths: [nestedIndex] }), | 
|  | 36 | +    path.join(nodeModules, 'bar.js') | 
|  | 37 | +  ); | 
|  | 38 | +} | 
|  | 39 | + | 
|  | 40 | +// Verify that the default paths can be used and modified. | 
|  | 41 | +{ | 
|  | 42 | +  const paths = require.resolve.paths('bar'); | 
|  | 43 | + | 
|  | 44 | +  assert.strictEqual(paths[0], nodeModules); | 
|  | 45 | +  assert.strictEqual( | 
|  | 46 | +    require.resolve('bar', { paths }), | 
|  | 47 | +    path.join(nodeModules, 'bar.js') | 
|  | 48 | +  ); | 
|  | 49 | + | 
|  | 50 | +  paths.unshift(nestedNodeModules); | 
|  | 51 | +  assert.strictEqual( | 
|  | 52 | +    require.resolve('bar', { paths }), | 
|  | 53 | +    path.join(nestedNodeModules, 'bar.js') | 
|  | 54 | +  ); | 
|  | 55 | +} | 
0 commit comments