Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/env/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ module.exports = {
const result = await moduleLoader.require(configPath);
return getModuleExports(result);
} catch (e) {
if (e.code === 'ERR_REQUIRE_ESM') {
if (e.code === 'ERR_REQUIRE_ESM' || e.code === 'ERR_REQUIRE_ASYNC_MODULE') {
const loadedImport = await moduleLoader.import(url.pathToFileURL(configPath));
return getModuleExports(loadedImport);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/env/migrationsDir.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ module.exports = {
const result = moduleLoader.require(migrationPath);
return getModuleExports(result);
} catch (e) {
if (e.code === 'ERR_REQUIRE_ESM') {
if (e.code === 'ERR_REQUIRE_ESM' || e.code === 'ERR_REQUIRE_ASYNC_MODULE') {
const loadedImport = moduleLoader.import(url.pathToFileURL(migrationPath));
return getModuleExports(loadedImport);
}
Expand Down
9 changes: 9 additions & 0 deletions test/env/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ describe("config", () => {
expect(moduleLoader.import.called).to.equal(true);
});

it("should fall back to using 'import' if Node requires the use of ESM (top-level await)", async () => {
const error = new Error('ESM required');
error.code = 'ERR_REQUIRE_ASYNC_MODULE';
moduleLoader.require = sinon.stub().throws(error);
moduleLoader.import.returns({});
await config.read();
expect(moduleLoader.import.called).to.equal(true);
});

it("should handle ESM modules with default export", async () => {
const expectedConfig = {
mongodb: {
Expand Down
9 changes: 9 additions & 0 deletions test/env/migrationsDir.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,15 @@ describe("migrationsDir", () => {
await migrationsDir.loadMigration("someFile.js");
expect(moduleLoader.import.called).to.equal(true);
});

it("should fall back to using 'import' if Node requires the use of ESM (top-level await)", async () => {
const error = new Error('ESM required');
error.code = 'ERR_REQUIRE_ASYNC_MODULE';
moduleLoader.require = sinon.stub().throws(error);
moduleLoader.import = sinon.stub().returns({ up: sinon.stub(), down: sinon.stub() });
await migrationsDir.loadMigration("someFile.js");
expect(moduleLoader.import.called).to.equal(true);
});
});

describe("resolveMigrationFileExtension()", () => {
Expand Down