• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * STOP!!! DO NOT MODIFY.
3 *
4 * This file is part of the ongoing work to move the eslintrc-style config
5 * system into the @eslint/eslintrc package. This file needs to remain
6 * unchanged in order for this work to proceed.
7 *
8 * If you think you need to change this file, please contact @nzakas first.
9 *
10 * Thanks in advance for your cooperation.
11 */
12
13/**
14 * Utility for resolving a module relative to another module
15 * @author Teddy Katz
16 */
17
18"use strict";
19
20const Module = require("module");
21
22/*
23 * `Module.createRequire` is added in v12.2.0. It supports URL as well.
24 * We only support the case where the argument is a filepath, not a URL.
25 */
26// eslint-disable-next-line node/no-unsupported-features/node-builtins, node/no-deprecated-api
27const createRequire = Module.createRequire || Module.createRequireFromPath;
28
29module.exports = {
30
31    /**
32     * Resolves a Node module relative to another module
33     * @param {string} moduleName The name of a Node module, or a path to a Node module.
34     * @param {string} relativeToPath An absolute path indicating the module that `moduleName` should be resolved relative to. This must be
35     * a file rather than a directory, but the file need not actually exist.
36     * @returns {string} The absolute path that would result from calling `require.resolve(moduleName)` in a file located at `relativeToPath`
37     */
38    resolve(moduleName, relativeToPath) {
39        try {
40            return createRequire(relativeToPath).resolve(moduleName);
41        } catch (error) {
42
43            // This `if` block is for older Node.js than 12.0.0. We can remove this block in the future.
44            if (
45                typeof error === "object" &&
46                error !== null &&
47                error.code === "MODULE_NOT_FOUND" &&
48                !error.requireStack &&
49                error.message.includes(moduleName)
50            ) {
51                error.message += `\nRequire stack:\n- ${relativeToPath}`;
52            }
53            throw error;
54        }
55    }
56};
57