MCPcopy Index your code
hub / github.com/nodejs/node / defaultResolve

Function defaultResolve

lib/internal/modules/esm/resolve.js:961–1032  ·  view source on GitHub ↗

* Resolves the given specifier using the provided context, which includes the parent URL and conditions. * Attempts to resolve the specifier and returns the resulting URL and format. * @param {string} specifier - The specifier to resolve. * @param {object} [context] - The context object containin

(specifier, context = kEmptyObject)

Source from the content-addressed store, hash-verified

959 * @returns {{url: string, format?: string}}
960 */
961function defaultResolve(specifier, context = kEmptyObject) {
962 let { parentURL, conditions } = context;
963 throwIfInvalidParentURL(parentURL);
964
965 let parsedParentURL;
966 if (parentURL) {
967 parsedParentURL = URLParse(parentURL);
968 }
969
970 let parsed, protocol;
971 if (shouldBeTreatedAsRelativeOrAbsolutePath(specifier)) {
972 parsed = URLParse(specifier, parsedParentURL);
973 } else {
974 parsed = URLParse(specifier);
975 }
976
977 if (parsed != null) {
978 // Avoid accessing the `protocol` property due to the lazy getters.
979 protocol = parsed.protocol;
980
981 if (protocol === 'data:') {
982 return { __proto__: null, url: parsed.href };
983 }
984 }
985
986 protocol ??= parsed?.protocol;
987 if (protocol === 'node:') { return { __proto__: null, url: specifier }; }
988
989
990 const isMain = parentURL === undefined;
991 if (isMain) {
992 parentURL = getCWDURL().href;
993
994 // This is the initial entry point to the program, and --input-type has
995 // been passed as an option; but --input-type can only be used with
996 // --eval, --print or STDIN string input. It is not allowed with file
997 // input, to avoid user confusion over how expansive the effect of the
998 // flag should be (i.e. entry point only, package scope surrounding the
999 // entry point, etc.).
1000 if (getOptionValue('--input-type')) { throw new ERR_INPUT_TYPE_NOT_ALLOWED(); }
1001 }
1002
1003 conditions = getConditionsSet(conditions);
1004 let url;
1005 try {
1006 url = moduleResolve(
1007 specifier,
1008 parentURL,
1009 conditions,
1010 isMain ? getOptionValue('--preserve-symlinks-main') : getOptionValue('--preserve-symlinks'),
1011 );
1012 } catch (error) {
1013 // Try to give the user a hint of what would have been the
1014 // resolved CommonJS module
1015 if (error.code === 'ERR_MODULE_NOT_FOUND' ||
1016 error.code === 'ERR_UNSUPPORTED_DIR_IMPORT') {
1017 if (StringPrototypeStartsWith(specifier, 'file://')) {
1018 specifier = fileURLToPath(specifier);

Callers 1

#cachedDefaultResolveMethod · 0.70

Calls 9

throwIfInvalidParentURLFunction · 0.85
getCWDURLFunction · 0.85
getOptionValueFunction · 0.85
getConditionsSetFunction · 0.85
moduleResolveFunction · 0.85
fileURLToPathFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…