* Resolve and evaluate it synchronously as ESM if it's ESM. * @param {Module} mod CJS module instance * @param {string} filename Absolute path of the file. * @param {string} format Format of the module. If it had types, this would be what it is after type-stripping. * @param {string} source Sour
(mod, filename, format, source)
| 1701 | * @param {string} source Source the module. If it had types, this would have the type stripped. |
| 1702 | */ |
| 1703 | function loadESMFromCJS(mod, filename, format, source) { |
| 1704 | const cascadedLoader = require('internal/modules/esm/loader').getOrInitializeCascadedLoader(); |
| 1705 | const isMain = mod[kIsMainSymbol]; |
| 1706 | if (isMain) { |
| 1707 | require('internal/modules/run_main').runEntryPointWithESMLoader((cascadedLoader) => { |
| 1708 | const mainURL = pathToFileURL(filename).href; |
| 1709 | return cascadedLoader.import(mainURL, undefined, { __proto__: null }, undefined, true); |
| 1710 | }); |
| 1711 | // ESM won't be accessible via process.mainModule. |
| 1712 | setOwnProperty(process, 'mainModule', undefined); |
| 1713 | } else { |
| 1714 | const parent = mod[kLastModuleParent]; |
| 1715 | |
| 1716 | requireModuleWarningMode ??= getOptionValue('--trace-require-module'); |
| 1717 | if (requireModuleWarningMode) { |
| 1718 | let shouldEmitWarning = false; |
| 1719 | if (requireModuleWarningMode === 'no-node-modules') { |
| 1720 | // Check if the require() comes from node_modules. |
| 1721 | if (parent) { |
| 1722 | shouldEmitWarning = !isUnderNodeModules(parent.filename); |
| 1723 | } else if (mod[kIsCachedByESMLoader]) { |
| 1724 | // It comes from the require() built for `import cjs` and doesn't have a parent recorded |
| 1725 | // in the CJS module instance. Inspect the stack trace to see if the require() |
| 1726 | // comes from node_modules as a direct call and reduce the noise. |
| 1727 | shouldEmitWarning = !isInsideNodeModules(); |
| 1728 | } |
| 1729 | } else { |
| 1730 | shouldEmitWarning = true; |
| 1731 | } |
| 1732 | if (shouldEmitWarning) { |
| 1733 | let messagePrefix; |
| 1734 | if (parent) { |
| 1735 | // In the case of the module calling `require()`, it's more useful to know its absolute path. |
| 1736 | let from = parent.filename || parent.id; |
| 1737 | // In the case of the module being require()d, it's more useful to know the id passed into require(). |
| 1738 | const to = mod.id || mod.filename; |
| 1739 | if (from === 'internal/preload') { |
| 1740 | from = '--require'; |
| 1741 | } else if (from === '<repl>') { |
| 1742 | from = 'The REPL'; |
| 1743 | } else if (from === '.') { |
| 1744 | from = 'The entry point'; |
| 1745 | } else { |
| 1746 | from &&= `CommonJS module ${from}`; |
| 1747 | } |
| 1748 | if (from && to) { |
| 1749 | messagePrefix = `${from} is loading ES Module ${to} using require().\n`; |
| 1750 | } |
| 1751 | } |
| 1752 | emitExperimentalWarning('Support for loading ES Module in require()', |
| 1753 | messagePrefix, |
| 1754 | undefined, |
| 1755 | parent?.require); |
| 1756 | requireModuleWarningMode = true; |
| 1757 | } |
| 1758 | } |
| 1759 | const { |
| 1760 | wrap, |
no test coverage detected
searching dependent graphs…