Skip to content
This repository was archived by the owner on Apr 16, 2020. It is now read-only.

Commit be696f2

Browse files
committed
esm: fix errors for --type / "type" mismatches
1 parent 723a478 commit be696f2

File tree

4 files changed

+23
-26
lines changed

4 files changed

+23
-26
lines changed

doc/api/esm.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,7 @@ PACKAGE_MAIN_RESOLVE(_packageURL_, _pjson_)
263263
> 1. Return _"commonjs"_.
264264
> 1. If _pjson.type_ exists and is _"module"_, then
265265
> 1. If _url_ ends in _".cjs"_, then
266-
> 1. Throw a _Type Mismatch_ error.
267-
> 1. If _url_ does not end in _".js"_ or _".mjs"_, then
268-
> 1. Throw an _Unsupported File Extension_ error.
266+
> 1. Return _"commonjs"_.
269267
> 1. Return _"module"_.
270268
> 1. Otherwise,
271269
> 1. If _url_ ends in _".mjs"_, then

lib/internal/errors.js

+11-13
Original file line numberDiff line numberDiff line change
@@ -956,21 +956,19 @@ E('ERR_TRANSFORM_ALREADY_TRANSFORMING',
956956
E('ERR_TRANSFORM_WITH_LENGTH_0',
957957
'Calling transform done when writableState.length != 0', Error);
958958
E('ERR_TTY_INIT_FAILED', 'TTY initialization failed', SystemError);
959-
E('ERR_TYPE_MISMATCH', (filename, moduleType, scopeConflict, extConflict) => {
960-
const typeValue = moduleType ? 'module' : 'commonjs';
959+
E('ERR_TYPE_MISMATCH', (filename, ext, typeFlag, conflict) => {
960+
const typeString =
961+
typeFlag === 'module' ? '--type=module or -m' : '--type=commonjs';
961962
// --type mismatches file extension
962-
if (!scopeConflict && extConflict)
963-
return `--type=${typeValue + (typeValue === 'module' ? ' / -m' : '')}` +
964-
` flag conflicts with the file extension loading ${filename}`;
963+
if (conflict === 'extension')
964+
return `Extension ${ext} is not supported for ` +
965+
`${typeString} loading ${filename}`;
965966
// --type mismatches package.json "type"
966-
else if (scopeConflict && !extConflict)
967-
return `--type=${typeValue + (typeValue === 'module' ? ' / ` -m' : '')}` +
968-
' flag conflicts with the package.json "type": "' +
969-
(moduleType ? 'commonjs' : 'module') + `" loading ${filename}`;
970-
// package.json "type" mismatches file extension
971-
else if (extConflict && scopeConflict)
972-
return `"type": "${typeValue}" in package.json conflicts with the file ` +
973-
`extension loading ${filename}`;
967+
else if (conflict === 'scope')
968+
return `Cannot use ${typeString} because nearest parent package.json ` +
969+
((typeFlag === 'module') ?
970+
'includes "type": "commonjs"' : 'includes "type": "module",') +
971+
` which controls the type to use for ${filename}`;
974972
}, TypeError);
975973
E('ERR_UNCAUGHT_EXCEPTION_CAPTURE_ALREADY_SET',
976974
'`process.setupUncaughtExceptionCapture()` was called while a capture ' +

lib/internal/modules/esm/default_resolve.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@ function getModuleFormat(url, isMain, parentURL) {
7575

7676
const ext = extname(url.pathname);
7777

78-
if (!legacy && ext === '.cjs')
79-
throw new ERR_TYPE_MISMATCH(true, true, fileURLToPath(url));
80-
8178
let format = (legacy ? legacyExtensionFormatMap : extensionFormatMap)[ext];
8279

8380
if (!format) {
@@ -89,14 +86,17 @@ function getModuleFormat(url, isMain, parentURL) {
8986
}
9087

9188
// Check for mismatch between --type and file extension,
92-
// and between --type and package.json.
89+
// and between --type and the "type" field in package.json.
9390
if (isMain && format !== 'module' && asyncESM.typeFlag === 'module') {
9491
// Conflict between package scope type and --type
9592
if (ext === '.js') {
96-
throw new ERR_TYPE_MISMATCH(fileURLToPath(url), true, true, false);
93+
if (pcfg && pcfg.type)
94+
throw new ERR_TYPE_MISMATCH(
95+
fileURLToPath(url), ext, asyncESM.typeFlag, 'scope');
9796
// Conflict between explicit extension (.mjs, .cjs) and --type
9897
} else {
99-
throw new ERR_TYPE_MISMATCH(fileURLToPath(url), true, false, true);
98+
throw new ERR_TYPE_MISMATCH(
99+
fileURLToPath(url), ext, asyncESM.typeFlag, 'extension');
100100
}
101101
}
102102

test/es-module/test-esm-type-flag-errors.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ expect('', packageTypeModuleMain, 'package-type-module');
1919
expect('', packageTypeCommonJsMain, 'package-type-commonjs');
2020
expect('', packageWithoutTypeMain, 'package-without-type');
2121

22+
// Check that running with --type and no package.json "type" works
23+
expect('--type=commonjs', packageWithoutTypeMain, 'package-without-type');
24+
expect('--type=module', packageWithoutTypeMain, 'package-without-type');
25+
expect('-m', packageWithoutTypeMain, 'package-without-type');
26+
2227
// Check that running with conflicting --type flags throws errors
2328
expect('--type=commonjs', mjsFile, 'ERR_REQUIRE_ESM', true);
2429
expect('--type=module', cjsFile, 'ERR_TYPE_MISMATCH', true);
@@ -29,10 +34,6 @@ expect('--type=module', packageTypeCommonJsMain,
2934
'ERR_TYPE_MISMATCH', true);
3035
expect('-m', packageTypeCommonJsMain,
3136
'ERR_TYPE_MISMATCH', true);
32-
expect('--type=module', packageWithoutTypeMain,
33-
'ERR_TYPE_MISMATCH', true);
34-
expect('-m', packageWithoutTypeMain,
35-
'ERR_TYPE_MISMATCH', true);
3637

3738
function expect(opt = '', inputFile, want, wantsError = false) {
3839
// TODO: Remove when --experimental-modules is unflagged

0 commit comments

Comments
 (0)