ESM_RESOLVE(specifier, parentURL)

    1. Let resolved be undefined.
    2. If specifier is a valid URL, then
      1. Set resolved to the result of parsing and reserializing specifier as a URL.
    3. Otherwise, if specifier starts with “/“, “./“ or “../“, then
      1. Set resolved to the URL resolution of specifier relative to parentURL.
    4. Otherwise, if specifier starts with “#”, then
      1. Set resolved to the destructured value of the result of PACKAGE_IMPORTS_RESOLVE(specifier, parentURL, defaultConditions).
    5. Otherwise,
      1. Note: specifier is now a bare specifier.
      2. Set resolved the result of PACKAGE_RESOLVE(specifier, parentURL).
    6. If resolved contains any percent encodings of “/“ or “\“ (“%2f” and “%5C” respectively), then
      1. Throw an Invalid Module Specifier error.
    7. If the file at resolved is a directory, then
      1. Throw an Unsupported Directory Import error.
    8. If the file at resolved does not exist, then
      1. Throw a Module Not Found error.
    9. Set resolved to the real path of resolved.
    10. Let format be the result of ESM_FORMAT(resolved).
    11. Load resolved as module format, format.
    12. Return resolved.

    PACKAGE_RESOLVE(packageSpecifier, parentURL)

    1. Let packageName be undefined.
    2. If packageSpecifier is an empty string, then
      1. Throw an Invalid Module Specifier error.
    3. If packageSpecifier does not start with “@”, then
      1. Set packageName to the substring of packageSpecifier until the first “/“ separator or the end of the string.
    4. Otherwise,
      1. If packageSpecifier does not contain a “/“ separator, then
        1. Throw an Invalid Module Specifier error.
      2. Set packageName to the substring of packageSpecifier until the second “/“ separator or the end of the string.
    5. If packageName starts with “.” or contains “\“ or “%”, then
      1. Throw an Invalid Module Specifier error.
    6. Let packageSubpath be “.” concatenated with the substring of packageSpecifier from the position at the length of packageName.
    7. Let selfUrl be the result of PACKAGE_SELF_RESOLVE(packageName, packageSubpath, parentURL).
    8. If selfUrl is not undefined, return selfUrl.
    9. If packageSubpath is “.” and packageName is a Node.js builtin module, then
      1. Return the string “node:” concatenated with packageSpecifier.
    10. While parentURL is not the file system root,
      1. Let packageURL be the URL resolution of “node_modules/“ concatenated with packageSpecifier, relative to parentURL.
      2. Set parentURL to the parent folder URL of parentURL.
      3. If the folder at packageURL does not exist, then
        1. Set parentURL to the parent URL path of parentURL.
        2. Continue the next loop iteration.
      4. Let pjson be the result of READ_PACKAGE_JSON(packageURL).
      5. If pjson is not null and pjson.exports is not null or undefined, then
        1. Let exports be pjson.exports.
        2. Return the resolved destructured value of the result of PACKAGE_EXPORTS_RESOLVE(packageURL, packageSubpath, pjson.exports, defaultConditions).
      6. Otherwise, if packageSubpath is equal to “.”, then
        1. Return the result applying the legacy LOAD_AS_DIRECTORY CommonJS resolver to packageURL, throwing a Module Not Found error for no resolution.
      7. Otherwise,
        1. Return the URL resolution of packageSubpath in packageURL.
    11. Throw a Module Not Found error.

    PACKAGE_SELF_RESOLVE(packageName, packageSubpath, parentURL)

    1. Let packageURL be the result of READ_PACKAGE_SCOPE(parentURL).
    2. If packageURL is null, then
      1. Return undefined.
    3. Let pjson be the result of READ_PACKAGE_JSON(packageURL).
    4. If pjson is null or if pjson.exports is null or undefined, then
      1. Return undefined.
    5. If pjson.name is equal to packageName, then
      1. Return the resolved destructured value of the result of PACKAGE_EXPORTS_RESOLVE(packageURL, subpath, pjson.exports, defaultConditions).
    6. Otherwise, return undefined.

    PACKAGE_EXPORTS_RESOLVE(packageURL, subpath, exports, conditions)

    1. If exports is an Object with both a key starting with “.” and a key not starting with “.”, throw an Invalid Package Configuration error.
    2. If subpath is equal to “.”, then
      1. Let mainExport be undefined.
      2. If exports is a String or Array, or an Object containing no keys starting with “.”, then
        1. Set mainExport to exports.
      3. Otherwise if exports is an Object containing a “.” property, then
        1. Set mainExport to exports[“.”].
      4. If mainExport is not undefined, then
        1. Let resolved be the result of PACKAGE_TARGET_RESOLVE( packageURL, mainExport, “”, false, false, conditions).
        2. If resolved is not null or undefined, then
          1. Return resolved.
    3. Otherwise, if exports is an Object and all keys of exports start with “.”, then
      1. Let matchKey be the string “./“ concatenated with subpath.
      2. Let resolvedMatch be result of PACKAGE_IMPORTS_EXPORTS_RESOLVE( matchKey, exports, packageURL, false, conditions).
      3. If resolvedMatch.resolve is not null or undefined, then
        1. Return resolvedMatch.
    4. Throw a Package Path Not Exported error.

    PACKAGE_IMPORTS_RESOLVE(specifier, parentURL, conditions)

    1. Assert: specifier begins with “#”.
    2. If specifier is exactly equal to “#” or starts with “#/“, then
      1. Throw an Invalid Module Specifier error.
    3. Let packageURL be the result of READ_PACKAGE_SCOPE(parentURL).
    4. If packageURL is not null, then
      1. Let pjson be the result of READ_PACKAGE_JSON(packageURL).
      2. If pjson.imports is a non-null Object, then
        1. Let resolvedMatch be the result of PACKAGE_IMPORTS_EXPORTS_RESOLVE(specifier, pjson.imports, packageURL, true, conditions).
        2. If resolvedMatch.resolve is not null or undefined, then
          1. Return resolvedMatch.
    5. Throw a Package Import Not Defined error.

    PACKAGE_IMPORTS_EXPORTS_RESOLVE(matchKey, matchObj, packageURL, isImports, conditions)

    1. If matchKey is a key of matchObj, and does not end in “*”, then
      1. Let target be the value of matchObj[matchKey].
      2. Let resolved be the result of PACKAGE_TARGET_RESOLVE( packageURL, target, “”, false, isImports, conditions).
      3. Return the object { resolved, exact: true }.
    2. Let expansionKeys be the list of keys of matchObj ending in “/“ or “*”, sorted by length descending.
    3. For each key expansionKey in expansionKeys, do
      1. If expansionKey ends in “*” and matchKey starts with but is not equal to the substring of expansionKey excluding the last “*” character, then
        1. Let target be the value of matchObj[expansionKey].
        2. Let subpath be the substring of matchKey starting at the index of the length of expansionKey minus one.
        3. Let resolved be the result of PACKAGE_TARGET_RESOLVE( packageURL, target, subpath, true, isImports, conditions).
        4. Return the object { resolved, exact: true }.
      2. If matchKey starts with expansionKey, then
        1. Let target be the value of matchObj[expansionKey].
        2. Let subpath be the substring of matchKey starting at the index of the length of expansionKey.
        3. Let resolved be the result of PACKAGE_TARGET_RESOLVE( packageURL, target, subpath, false, isImports, conditions).
        4. Return the object { resolved, exact: false }.
    4. Return the object { resolved: null, exact: true }.

    PACKAGE_TARGET_RESOLVE(packageURL, target, subpath, pattern, internal, conditions)

    1. If target is a String, then
      1. If pattern is false, subpath has non-zero length and target does not end with “/“, throw an Invalid Module Specifier error.
      2. If target does not start with “./“, then
        1. If internal is true and target does not start with “../“ or “/“ and is not a valid URL, then
          1. If pattern is true, then
            1. Return PACKAGE_RESOLVE(target with every instance of “*” replaced by subpath, packageURL + “/“)_.
          2. Return PACKAGE_RESOLVE(target + subpath, packageURL + “/“)_.
        2. Otherwise, throw an Invalid Package Target error.
      3. If target split on “/“ or “\“ contains any “.”, “..” or “node_modules” segments after the first segment, throw an Invalid Package Target error.
      4. Let resolvedTarget be the URL resolution of the concatenation of packageURL and target.
      5. Assert: resolvedTarget is contained in packageURL.
      6. If subpath split on “/“ or “\“ contains any “.”, “..” or “node_modules” segments, throw an Invalid Module Specifier error.
      7. If pattern is true, then
        1. Return the URL resolution of resolvedTarget with every instance of “*” replaced with subpath.
      8. Otherwise,
        1. Return the URL resolution of the concatenation of subpath and resolvedTarget.
    2. Otherwise, if target is a non-null Object, then
      1. If exports contains any index property keys, as defined in ECMA-262 [6.1.7 Array Index][], throw an Invalid Package Configuration error.
      2. For each property p of target, in object insertion order as,
        1. If p equals “default” or conditions contains an entry for p, then
          1. Let targetValue be the value of the p property in target.
          2. Let resolved be the result of PACKAGE_TARGET_RESOLVE( packageURL, targetValue, subpath, pattern, internal, conditions).
          3. If resolved is equal to undefined, continue the loop.
          4. Return resolved.
      3. Return undefined.
    3. Otherwise, if target is an Array, then
      1. If _target.length is zero, return null.
      2. For each item targetValue in target, do
        1. Let resolved be the result of PACKAGE_TARGET_RESOLVE( packageURL, targetValue, subpath, pattern, internal, conditions), continuing the loop on any Invalid Package Target error.
        2. If resolved is undefined, continue the loop.
        3. Return resolved.
      3. Return or throw the last fallback resolution null return or error.
    4. Otherwise, if target is null, return null.
    5. Otherwise throw an Invalid Package Target error.

    ESM_FORMAT(url)

    1. Assert: url corresponds to an existing file.
    2. Let pjson be the result of READ_PACKAGE_SCOPE(url).
    3. If url ends in “.mjs”, then
      1. Return “module”.
    4. If url ends in “.cjs”, then
      1. Return “commonjs”.
    5. If pjson?.type exists and is “module”, then
      1. If url ends in “.js”, then
        1. Return “module”.
      2. Throw an Unsupported File Extension error.
    6. Otherwise,
      1. Throw an Unsupported File Extension error.

    READ_PACKAGE_SCOPE(url)

    1. Let scopeURL be url.
    2. While scopeURL is not the file system root,
      1. Set scopeURL to the parent URL of scopeURL.
      2. If scopeURL ends in a “node_modules” path segment, return null.
      3. Let pjson be the result of READ_PACKAGE_JSON(scopeURL).
      4. If pjson is not null, then
        1. Return pjson.
    3. Return null.

    READ_PACKAGE_JSON(packageURL)

    1. Let pjsonURL be the resolution of “package.json” within packageURL.
    2. If the file at pjsonURL does not exist, then
      1. Return null.
    3. If the file at packageURL does not parse as valid JSON, then
      1. Throw an Invalid Package Configuration error.
    4. Return the parsed JSON source of the file at pjsonURL.