From c30fcb3e72e3fdb76b3ea7a5f12ecb2d50f1c47b Mon Sep 17 00:00:00 2001 From: Nick Chevsky Date: Tue, 24 Jan 2023 17:35:44 -0600 Subject: [PATCH 1/3] Fix ESM imports on Windows When `import()`ing an ES module from the `requireModule()` utility function, convert the `fullpath` argument to a `file://` URL as expected by Node.js' ESM loader. This resolves error `ERR_UNSUPPORTED_ESM_URL_SCHEME` on Windows, where absolute paths are preceded by a drive letter and cannot be interpreted as URLs. --- lib/utils/requireModule.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/utils/requireModule.js b/lib/utils/requireModule.js index aa7c1e15b5..d18f597d0a 100644 --- a/lib/utils/requireModule.js +++ b/lib/utils/requireModule.js @@ -1,3 +1,5 @@ +const { pathToFileURL } = require('node:url'); + module.exports = function (fullpath) { let exported; try { @@ -15,7 +17,7 @@ module.exports = function (fullpath) { throw err; } - return import(fullpath).then(result => (result.default || {})); + return import(pathToFileURL(fullpath).href).then(result => (result.default || {})); } if (exported && Object.prototype.hasOwnProperty.call(exported, 'default') && Object.keys(exported).length === 1) { From 98fe6e09830ad4262f8c4492a75908e3a7ae1ad7 Mon Sep 17 00:00:00 2001 From: Andrei Rusu Date: Sun, 29 Jan 2023 21:09:58 +0100 Subject: [PATCH 2/3] Update requireModule.js --- lib/utils/requireModule.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/utils/requireModule.js b/lib/utils/requireModule.js index d18f597d0a..3324495426 100644 --- a/lib/utils/requireModule.js +++ b/lib/utils/requireModule.js @@ -1,4 +1,9 @@ -const { pathToFileURL } = require('node:url'); +const {pathToFileURL} = require('node:url'); +const getNodeVersion = () => { + const version = process.version.replace('v', ''); + + return parseInt(version, 10); +} module.exports = function (fullpath) { let exported; @@ -17,7 +22,11 @@ module.exports = function (fullpath) { throw err; } - return import(pathToFileURL(fullpath).href).then(result => (result.default || {})); + if (getNodeVersion() >= 14) { + return import(pathToFileURL(fullpath).href).then(result => (result.default || {})); + } + + return import(fullpath).then(result => (result.default || {})); } if (exported && Object.prototype.hasOwnProperty.call(exported, 'default') && Object.keys(exported).length === 1) { From d4ba53c070b01662717426bf42822e5d60da8dc7 Mon Sep 17 00:00:00 2001 From: Andrei Rusu Date: Sun, 29 Jan 2023 21:19:50 +0100 Subject: [PATCH 3/3] Update requireModule.js --- lib/utils/requireModule.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/utils/requireModule.js b/lib/utils/requireModule.js index 3324495426..f65315e8d4 100644 --- a/lib/utils/requireModule.js +++ b/lib/utils/requireModule.js @@ -1,4 +1,3 @@ -const {pathToFileURL} = require('node:url'); const getNodeVersion = () => { const version = process.version.replace('v', ''); @@ -23,6 +22,8 @@ module.exports = function (fullpath) { } if (getNodeVersion() >= 14) { + const {pathToFileURL} = require('node:url'); + return import(pathToFileURL(fullpath).href).then(result => (result.default || {})); }