* Generate the legacy ERR_REQUIRE_ESM for the cases where require(esm) is disabled. * @param {Module} mod The module being required. * @param {undefined|object} pkg Data of the nearest package.json of the module. * @param {string} content Source code of the module. * @param {string} filename Fil
(mod, pkg, content, filename)
| 2008 | * @returns {Error} |
| 2009 | */ |
| 2010 | function getRequireESMError(mod, pkg, content, filename) { |
| 2011 | // This is an error path because `require` of a `.js` file in a `"type": "module"` scope is not allowed. |
| 2012 | const parent = mod[kFirstModuleParent]; |
| 2013 | const parentPath = parent?.filename; |
| 2014 | const packageJsonPath = pkg?.path; |
| 2015 | const usesEsm = containsModuleSyntax(content, filename); |
| 2016 | const err = new ERR_REQUIRE_ESM(filename, usesEsm, parentPath, |
| 2017 | packageJsonPath); |
| 2018 | // Attempt to reconstruct the parent require frame. |
| 2019 | const parentModule = Module._cache[parentPath]; |
| 2020 | if (parentModule) { |
| 2021 | let parentSource; |
| 2022 | try { |
| 2023 | ({ source: parentSource } = loadSource(parentModule, parentPath)); |
| 2024 | } catch { |
| 2025 | // Continue regardless of error. |
| 2026 | } |
| 2027 | if (parentSource) { |
| 2028 | // TODO(joyeecheung): trim off internal frames from the stack. |
| 2029 | reconstructErrorStack(err, parentPath, parentSource); |
| 2030 | } |
| 2031 | } |
| 2032 | return err; |
| 2033 | } |
| 2034 | |
| 2035 | /** |
| 2036 | * Built-in handler for `.js` files. |
no test coverage detected
searching dependent graphs…