diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index 4c50c4aa8a23f3..b7d9f7556e2289 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -373,11 +373,8 @@ class ModuleLoader { defaultLoadSync ??= require('internal/modules/esm/load').defaultLoadSync; const loadResult = defaultLoadSync(url, { format, importAttributes }); - let finalFormat = loadResult.format; // Use the synchronous commonjs translator which can deal with cycles. - if (finalFormat === 'commonjs') { - finalFormat = 'commonjs-sync'; - } + let finalFormat = loadResult.format === 'commonjs' ? 'commonjs-sync' : loadResult.format; if (finalFormat === 'wasm') { assert.fail('WASM is currently unsupported by require(esm)'); diff --git a/test/es-module/test-cjs-esm-warn.js b/test/es-module/test-cjs-esm-warn.js index 44bce720f2c38a..7ba19a8c954c6f 100644 --- a/test/es-module/test-cjs-esm-warn.js +++ b/test/es-module/test-cjs-esm-warn.js @@ -1,6 +1,6 @@ -// This used to test require(esm) throws ERR_REQUIRE_ESM, which is no longer the case -// since require(esm) has been implemented. It's now repurposed to test that the -// previous behavior can be kept when --no-experimental-require-module is passed. +// Previously, this tested that require(esm) throws ERR_REQUIRE_ESM, which is no longer applicable +// since require(esm) is now supported. The test has been repurposed to ensure that the old behavior +// is preserved when the --no-experimental-require-module flag is used. 'use strict'; const { spawnPromisified } = require('../common'); diff --git a/test/es-module/test-esm-type-field-errors-2.js b/test/es-module/test-esm-type-field-errors-2.js index b0cb5068e233fd..a67202149f9bab 100644 --- a/test/es-module/test-esm-type-field-errors-2.js +++ b/test/es-module/test-esm-type-field-errors-2.js @@ -1,15 +1,15 @@ // Flags: --no-experimental-require-module -// This used to test require(esm) throws ERR_REQUIRE_ESM, which is no longer the case -// since require(esm) has been implemented. It's now repurposed to test that the -// previous behavior can be kept when --no-experimental-require-module is passed. +// Previously, this tested that require(esm) throws ERR_REQUIRE_ESM, which is no longer applicable +// since require(esm) is now supported. The test has been repurposed to ensure that the old behavior +// is preserved when the --no-experimental-require-module flag is used. 'use strict'; require('../common'); const assert = require('assert'); const { describe, it } = require('node:test'); -describe('ESM type field errors', () => { - it('Expect CJS to fail loading from type: module package.', () => { +describe('Errors related to ESM type field', () => { + it('Should throw an error when loading CJS from a `type: "module"` package.', () => { assert.throws(() => require('../fixtures/es-modules/package-type-module/index.js'), { code: 'ERR_REQUIRE_ESM' }); diff --git a/test/es-module/test-require-module-preload.js b/test/es-module/test-require-module-preload.js index ca236bd24c6ff2..65ec1a93bc9d4b 100644 --- a/test/es-module/test-require-module-preload.js +++ b/test/es-module/test-require-module-preload.js @@ -6,10 +6,8 @@ const { fixturesDir } = require('../common/fixtures'); const warningRE = /ExperimentalWarning: Support for loading ES Module in require/; function testPreload(preloadFlag) { - let stderr = warningRE; - if (preloadFlag == '--import') { - stderr = undefined; // The warning is only emitted when ESM is loaded by --require. - } + // The warning is only emitted when ESM is loaded by --require. + const stderr = preloadFlag !== '--import' ? warningRE : undefined; // Test named exports. { diff --git a/test/parallel/test-require-mjs.js b/test/parallel/test-require-mjs.js index 607633bc4fd2c3..30bbf1da8deb7a 100644 --- a/test/parallel/test-require-mjs.js +++ b/test/parallel/test-require-mjs.js @@ -1,7 +1,7 @@ // Flags: --no-experimental-require-module -// This used to test require(esm) throws ERR_REQUIRE_ESM, which is no longer the case -// since require(esm) has been implemented. It's now repurposed to test that the -// previous behavior can be kept when --no-experimental-require-module is passed. +// Previously, this tested that require(esm) throws ERR_REQUIRE_ESM, which is no longer applicable +// since require(esm) is now supported. The test has been repurposed to ensure that the old behavior +// is preserved when the --no-experimental-require-module flag is used. 'use strict'; require('../common');