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

Function shouldUseESMLoader

lib/internal/modules/run_main.js:52–84  ·  view source on GitHub ↗

* Determine whether the main entry point should be loaded through the ESM Loader. * @param {string} mainPath - Absolute path to the main entry point * @returns {boolean}

(mainPath)

Source from the content-addressed store, hash-verified

50 * @returns {boolean}
51 */
52function shouldUseESMLoader(mainPath) {
53 /**
54 * @type {string[]} userLoaders A list of custom loaders registered by the user
55 * (or an empty list when none have been registered).
56 */
57 const userLoaders = getOptionValue('--experimental-loader');
58 /**
59 * @type {string[]} userImports A list of preloaded modules registered by the user
60 * (or an empty list when none have been registered).
61 */
62 const userImports = getOptionValue('--import');
63 if (userLoaders.length > 0 || userImports.length > 0) { return true; }
64
65 // Determine the module format of the entry point.
66 if (mainPath && StringPrototypeEndsWith(mainPath, '.mjs')) { return true; }
67 if (mainPath && StringPrototypeEndsWith(mainPath, '.wasm')) { return true; }
68 if (!mainPath || StringPrototypeEndsWith(mainPath, '.cjs')) { return false; }
69
70 if (getOptionValue('--strip-types')) {
71 if (!mainPath || StringPrototypeEndsWith(mainPath, '.cts')) { return false; }
72 // This will likely change in the future to start with commonjs loader by default
73 if (mainPath && StringPrototypeEndsWith(mainPath, '.mts')) { return true; }
74 }
75
76 const type = getNearestParentPackageJSONType(mainPath);
77
78 // No package.json or no `type` field.
79 if (type === undefined || type === 'none') {
80 return false;
81 }
82
83 return type === 'module';
84}
85
86/**
87 * @param {function(ModuleLoader):ModuleWrap|undefined} callback

Callers 1

executeUserEntryPointFunction · 0.85

Calls 1

getOptionValueFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…