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
9 changes: 6 additions & 3 deletions lib/internal/modules/esm/get_format.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,18 @@ 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';
} else {
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');
Expand All @@ -73,7 +76,7 @@ const protocolHandlers = ObjectAssign(ObjectCreate(null), {
}
}

return format || null;
return format;
},
'node:'() { return 'builtin'; },
});
Expand Down
14 changes: 14 additions & 0 deletions test/es-module/test-esm-get-format.js
Original file line number Diff line number Diff line change
@@ -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/ });
}