diff --git a/lib/esm-utils.js b/lib/esm-utils.js index e94580eea8..604f883d9a 100644 --- a/lib/esm-utils.js +++ b/lib/esm-utils.js @@ -1,8 +1,16 @@ const path = require('path'); +const url = require('url'); + +const formattedImport = async file => { + if (path.isAbsolute(file)) { + return import(url.pathToFileURL(file)); + } + return import(file); +}; exports.requireOrImport = async file => { if (path.extname(file) === '.mjs') { - return import(file); + return formattedImport(file); } // This is currently the only known way of figuring out whether a file is CJS or ESM. // If Node.js or the community establish a better procedure for that, we can fix this code. @@ -12,7 +20,7 @@ exports.requireOrImport = async file => { return require(file); } catch (err) { if (err.code === 'ERR_REQUIRE_ESM') { - return import(file); + return formattedImport(file); } else { throw err; }