• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23const common = require('../common');
24const fixtures = require('../common/fixtures');
25const assert = require('assert');
26const { builtinModules } = require('module');
27const path = require('path');
28
29assert.strictEqual(
30  require.resolve(fixtures.path('a')).toLowerCase(),
31  fixtures.path('a.js').toLowerCase());
32assert.strictEqual(
33  require.resolve(fixtures.path('nested-index', 'one')).toLowerCase(),
34  fixtures.path('nested-index', 'one', 'index.js').toLowerCase());
35assert.strictEqual(require.resolve('path'), 'path');
36
37// Test configurable resolve() paths.
38require(fixtures.path('require-resolve.js'));
39require(fixtures.path('resolve-paths', 'default', 'verify-paths.js'));
40
41[1, false, null, undefined, {}].forEach((value) => {
42  const message = 'The "request" argument must be of type string.' +
43    common.invalidArgTypeHelper(value);
44  assert.throws(
45    () => { require.resolve(value); },
46    {
47      code: 'ERR_INVALID_ARG_TYPE',
48      message
49    });
50
51  assert.throws(
52    () => { require.resolve.paths(value); },
53    {
54      code: 'ERR_INVALID_ARG_TYPE',
55      message
56    });
57});
58
59// Test require.resolve.paths.
60{
61  // builtinModules.
62  builtinModules.forEach((mod) => {
63    assert.strictEqual(require.resolve.paths(mod), null);
64  });
65
66  builtinModules.forEach((mod) => {
67    assert.strictEqual(require.resolve.paths(`node:${mod}`), null);
68  });
69
70  // node_modules.
71  const resolvedPaths = require.resolve.paths('eslint');
72  assert.strictEqual(Array.isArray(resolvedPaths), true);
73  assert.strictEqual(resolvedPaths[0].includes('node_modules'), true);
74
75  // relativeModules.
76  const relativeModules = ['.', '..', './foo', '../bar'];
77  relativeModules.forEach((mod) => {
78    const resolvedPaths = require.resolve.paths(mod);
79    assert.strictEqual(Array.isArray(resolvedPaths), true);
80    assert.strictEqual(resolvedPaths.length, 1);
81    assert.strictEqual(resolvedPaths[0], path.dirname(__filename));
82
83    // Shouldn't look up relative modules from 'node_modules'.
84    assert.strictEqual(resolvedPaths.includes('/node_modules'), false);
85  });
86}
87
88{
89  assert.strictEqual(require.resolve('node:test'), 'node:test');
90  assert.strictEqual(require.resolve('node:fs'), 'node:fs');
91
92  assert.throws(
93    () => require.resolve('node:unknown'),
94    { code: 'MODULE_NOT_FOUND' },
95  );
96}
97