From 94169d4abb9686d592e0d9dc0366b9907973906a Mon Sep 17 00:00:00 2001 From: Drew Batshaw Date: Tue, 10 Dec 2024 12:42:08 -0800 Subject: [PATCH 1/3] handle esm module import for newer versions of node that don't throw error on require --- lib/env/config.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/env/config.js b/lib/env/config.js index 5ff4a303..320a1d0b 100644 --- a/lib/env/config.js +++ b/lib/env/config.js @@ -63,13 +63,19 @@ module.exports = { } const configPath = getConfigPath(); try { - return await Promise.resolve(moduleLoader.require(configPath)); + const result = await moduleLoader.require(configPath); + return getModuleExports(result); } catch (e) { if (e.code === 'ERR_REQUIRE_ESM') { const loadedImport = await moduleLoader.import(url.pathToFileURL(configPath)); - return loadedImport.default + return getModuleExports(loadedImport); } throw e; } } }; + +function getModuleExports(module) { + // If ESM module format need to return default export + return module.default ? module.default : module; +} From 535cc3af6b24cd0cc17d2ba2e806e03ad682c172 Mon Sep 17 00:00:00 2001 From: Drew Batshaw Date: Tue, 10 Dec 2024 13:38:13 -0800 Subject: [PATCH 2/3] add specs for esm loading --- test/env/config.test.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/test/env/config.test.js b/test/env/config.test.js index bacd7dcb..e8cbb070 100644 --- a/test/env/config.test.js +++ b/test/env/config.test.js @@ -142,5 +142,35 @@ describe("config", () => { await config.read(); expect(moduleLoader.import.called).to.equal(true); }); + + it("should handle ESM modules with default export", async () => { + const expectedConfig = { + mongodb: { + url: 'mongodb://localhost:27017', + databaseName: 'test' + } + }; + + moduleLoader.require = sinon.stub().resolves({ + default: expectedConfig + }); + + const actual = await config.read(); + expect(actual).to.deep.equal(expectedConfig); + }); + + it("should handle regular CommonJS modules", async () => { + const expectedConfig = { + mongodb: { + url: 'mongodb://localhost:27017', + databaseName: 'test' + } + }; + + moduleLoader.require = sinon.stub().resolves(expectedConfig); + + const actual = await config.read(); + expect(actual).to.deep.equal(expectedConfig); + }); }); }); From ccff385f41067e9c2bc316b866bb368a9f499bae Mon Sep 17 00:00:00 2001 From: Drew Batshaw Date: Tue, 10 Dec 2024 13:49:18 -0800 Subject: [PATCH 3/3] fix es-lint error --- lib/env/config.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/env/config.js b/lib/env/config.js index 320a1d0b..d85d7973 100644 --- a/lib/env/config.js +++ b/lib/env/config.js @@ -20,6 +20,11 @@ function getConfigPath() { return path.join(process.cwd(), fileOptionValue); } +function getModuleExports(module) { + // If ESM module format need to return default export + return module.default ? module.default : module; +} + module.exports = { DEFAULT_CONFIG_FILE_NAME, @@ -74,8 +79,3 @@ module.exports = { } } }; - -function getModuleExports(module) { - // If ESM module format need to return default export - return module.default ? module.default : module; -}