diff --git a/lib/internal/modules/esm/get_format.js b/lib/internal/modules/esm/get_format.js index 7c07e5b1f7ce17..0683113f0106db 100644 --- a/lib/internal/modules/esm/get_format.js +++ b/lib/internal/modules/esm/get_format.js @@ -55,7 +55,7 @@ const protocolHandlers = ObjectAssign(ObjectCreate(null), { }, 'file:'(parsed, url) { const ext = extname(parsed.pathname); - let format; + let format = null; if (ext === '.js') { format = getPackageType(parsed.href) === 'module' ? 'module' : 'commonjs'; @@ -63,7 +63,10 @@ const protocolHandlers = ObjectAssign(ObjectCreate(null), { format = extensionFormatMap[ext]; } if (!format) { - if (experimentalSpecifierResolution === 'node') { + if ( + experimentalSpecifierResolution === 'node' && + legacyExtensionFormatMap[ext] + ) { process.emitWarning( 'The Node.js specifier resolution in ESM is experimental.', 'ExperimentalWarning'); @@ -73,7 +76,7 @@ const protocolHandlers = ObjectAssign(ObjectCreate(null), { } } - return format || null; + return format; }, 'node:'() { return 'builtin'; }, }); diff --git a/test/es-module/test-esm-get-format.js b/test/es-module/test-esm-get-format.js new file mode 100644 index 00000000000000..e711b57615c3a4 --- /dev/null +++ b/test/es-module/test-esm-get-format.js @@ -0,0 +1,14 @@ +'use strict'; +require('../common'); +const assert = require('assert'); +const { defaultGetFormat } = require('internal/modules/esm/get_format'); + +{ + const url = new URL('file://example.com/foo/bar.js'); + assert.strictEqual(defaultGetFormat(url), 'commonjs'); +} + +{ + const url = new URL('file://example.com/foo/bar.whatever'); + assert.throws(() => defaultGetFormat(url), { name: 'TypeError', message: /Unknown file extension whatever/ }); +}