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

Commit cb4203b

Browse files
GeoffreyBoothnodejs-ci
authored andcommitted
esm: merge both 'type mismatch' errors into one
1 parent 02b8175 commit cb4203b

File tree

4 files changed

+29
-38
lines changed

4 files changed

+29
-38
lines changed

doc/api/errors.md

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2218,22 +2218,16 @@ while trying to read and parse it.
22182218
22192219
The `--type=...` flag is not compatible with the Node.js REPL.
22202220

2221-
<a id="ERR_INVALID_TYPE_EXTENSION"></a>
2222-
### ERR_INVALID_TYPE_EXTENSION
2221+
<a id="ERR_TYPE_MISMATCH"></a>
2222+
### ERR_TYPE_MISMATCH
22232223

22242224
> Stability: 1 - Experimental
22252225
2226-
Attempted to execute a `.cjs` file with the `--type=module` flag,
2227-
or an `.mjs` file with the `--type=commonjs` flag.
2228-
2229-
<a id="ERR_INVALID_TYPE_IN_PACKAGE_SCOPE"></a>
2230-
### ERR_INVALID_TYPE_IN_PACKAGE_SCOPE
2231-
2232-
> Stability: 1 - Experimental
2233-
2234-
Attempted to execute a `.js` file with the `--type=commonjs` flag where the
2235-
nearest `package.json` contains `"type": "module"`; or a `.js` file with the
2236-
`--type=module` flag where the nearest `package.json` either lacks a `"type"`
2226+
The `--type=commonjs` flag was used to attempt to execute an `.mjs` file or
2227+
a `.js` file where the nearest parent `package.json` contains
2228+
`"type": "module"`; or
2229+
the `--type=module` flag was used to attempt to execute a `.cjs` file or
2230+
a `.js` file where the nearest parent `package.json` either lacks a `"type"`
22372231
field or contains `"type": "commonjs"`.
22382232

22392233
<a id="ERR_INVALID_TYPE_FLAG"></a>

lib/internal/errors.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -815,21 +815,9 @@ E('ERR_INVALID_SYNC_FORK_INPUT',
815815
TypeError);
816816
E('ERR_INVALID_THIS', 'Value of "this" must be of type %s', TypeError);
817817
E('ERR_INVALID_TUPLE', '%s must be an iterable %s tuple', TypeError);
818-
E('ERR_INVALID_TYPE_EXTENSION', (extension, flagValue) => {
819-
if (flagValue === 'module')
820-
flagValue = 'module or -m';
821-
return `${extension} is not supported for --type=${flagValue}`;
822-
}, TypeError);
823818
E('ERR_INVALID_TYPE_FLAG',
824819
'Type flag must be one of "module", "commonjs". Received --type=%s',
825820
TypeError);
826-
E('ERR_INVALID_TYPE_IN_PACKAGE_SCOPE', (flagValue, scopeType) => {
827-
if (flagValue === 'module')
828-
flagValue = 'module or -m';
829-
return `Cannot use --type=${flagValue} because nearest parent package.json` +
830-
` includes "type": "${scopeType}"${(scopeType === 'commonjs') ?
831-
' (or lacks the "type" field)' : ''}`;
832-
}, TypeError);
833821
E('ERR_INVALID_URI', 'URI malformed', URIError);
834822
E('ERR_INVALID_URL', 'Invalid URL: %s', TypeError);
835823
E('ERR_INVALID_URL_SCHEME',
@@ -968,6 +956,16 @@ E('ERR_TRANSFORM_ALREADY_TRANSFORMING',
968956
E('ERR_TRANSFORM_WITH_LENGTH_0',
969957
'Calling transform done when writableState.length != 0', Error);
970958
E('ERR_TTY_INIT_FAILED', 'TTY initialization failed', SystemError);
959+
E('ERR_TYPE_MISMATCH', (flagValue, extension, scopeType) => {
960+
if (flagValue === 'module')
961+
flagValue = 'module or -m';
962+
if (extension) // Mismatch between --type and file extension
963+
return `${extension} is not supported for --type=${flagValue}`;
964+
else // Mismatch between --type and package.json "type" field
965+
return `Cannot use --type=${flagValue} because nearest parent ` +
966+
`package.json includes "type": "${scopeType}"${(scopeType === 'commonjs') ?
967+
' (or lacks the "type" field)' : ''}`;
968+
}, TypeError);
971969
E('ERR_UNCAUGHT_EXCEPTION_CAPTURE_ALREADY_SET',
972970
'`process.setupUncaughtExceptionCapture()` was called while a capture ' +
973971
'callback was already active',

lib/internal/modules/cjs/loader.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ const { compileFunction } = internalBinding('contextify');
5151

5252
const {
5353
ERR_INVALID_ARG_VALUE,
54-
ERR_INVALID_TYPE_EXTENSION,
55-
ERR_INVALID_TYPE_IN_PACKAGE_SCOPE,
56-
ERR_REQUIRE_ESM
54+
ERR_REQUIRE_ESM,
55+
ERR_TYPE_MISMATCH
5756
} = require('internal/errors').codes;
5857
const { validateString } = require('internal/validators');
5958

@@ -874,10 +873,10 @@ Module.runMain = function() {
874873
(!isModule && asyncESM.typeFlag === 'module')) {
875874
if (ext === '.js') {
876875
// Conflict between package scope type and --type
877-
throw new ERR_INVALID_TYPE_IN_PACKAGE_SCOPE(asyncESM.typeFlag, format);
876+
throw new ERR_TYPE_MISMATCH(asyncESM.typeFlag, undefined, format);
878877
} else {
879878
// Conflict between explicit extension (.mjs, .cjs) and --type
880-
throw new ERR_INVALID_TYPE_EXTENSION(ext, asyncESM.typeFlag);
879+
throw new ERR_TYPE_MISMATCH(asyncESM.typeFlag, ext, undefined);
881880
}
882881
}
883882

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ expect('', packageTypeCommonJsMain, 'package-type-commonjs');
2020
expect('', packageWithoutTypeMain, 'package-without-type');
2121

2222
// Check that running with conflicting --type flags throws errors
23-
expect('--type=commonjs', mjsFile, 'ERR_INVALID_TYPE_EXTENSION', true);
24-
expect('--type=module', cjsFile, 'ERR_INVALID_TYPE_EXTENSION', true);
25-
expect('-m', cjsFile, 'ERR_INVALID_TYPE_EXTENSION', true);
23+
expect('--type=commonjs', mjsFile, 'ERR_TYPE_MISMATCH', true);
24+
expect('--type=module', cjsFile, 'ERR_TYPE_MISMATCH', true);
25+
expect('-m', cjsFile, 'ERR_TYPE_MISMATCH', true);
2626
expect('--type=commonjs', packageTypeModuleMain,
27-
'ERR_INVALID_TYPE_IN_PACKAGE_SCOPE', true);
27+
'ERR_TYPE_MISMATCH', true);
2828
expect('--type=module', packageTypeCommonJsMain,
29-
'ERR_INVALID_TYPE_IN_PACKAGE_SCOPE', true);
29+
'ERR_TYPE_MISMATCH', true);
3030
expect('-m', packageTypeCommonJsMain,
31-
'ERR_INVALID_TYPE_IN_PACKAGE_SCOPE', true);
31+
'ERR_TYPE_MISMATCH', true);
3232
expect('--type=module', packageWithoutTypeMain,
33-
'ERR_INVALID_TYPE_IN_PACKAGE_SCOPE', true);
33+
'ERR_TYPE_MISMATCH', true);
3434
expect('-m', packageWithoutTypeMain,
35-
'ERR_INVALID_TYPE_IN_PACKAGE_SCOPE', true);
35+
'ERR_TYPE_MISMATCH', true);
3636

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

0 commit comments

Comments
 (0)