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

Commit 210cbc2

Browse files
committed
esm: merge both 'type mismatch' errors into one
1 parent 2b6e1e5 commit 210cbc2

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
@@ -2213,22 +2213,16 @@ while trying to read and parse it.
22132213
22142214
The `--type=...` flag is not compatible with the Node.js REPL.
22152215

2216-
<a id="ERR_INVALID_TYPE_EXTENSION"></a>
2217-
### ERR_INVALID_TYPE_EXTENSION
2216+
<a id="ERR_TYPE_MISMATCH"></a>
2217+
### ERR_TYPE_MISMATCH
22182218

22192219
> Stability: 1 - Experimental
22202220
2221-
Attempted to execute a `.cjs` file with the `--type=module` flag,
2222-
or an `.mjs` file with the `--type=commonjs` flag.
2223-
2224-
<a id="ERR_INVALID_TYPE_IN_PACKAGE_SCOPE"></a>
2225-
### ERR_INVALID_TYPE_IN_PACKAGE_SCOPE
2226-
2227-
> Stability: 1 - Experimental
2228-
2229-
Attempted to execute a `.js` file with the `--type=commonjs` flag where the
2230-
nearest `package.json` contains `"type": "module"`; or a `.js` file with the
2231-
`--type=module` flag where the nearest `package.json` either lacks a `"type"`
2221+
The `--type=commonjs` flag was used to attempt to execute an `.mjs` file or
2222+
a `.js` file where the nearest parent `package.json` contains
2223+
`"type": "module"`; or
2224+
the `--type=module` flag was used to attempt to execute a `.cjs` file or
2225+
a `.js` file where the nearest parent `package.json` either lacks a `"type"`
22322226
field or contains `"type": "commonjs"`.
22332227

22342228
<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
@@ -814,21 +814,9 @@ E('ERR_INVALID_SYNC_FORK_INPUT',
814814
TypeError);
815815
E('ERR_INVALID_THIS', 'Value of "this" must be of type %s', TypeError);
816816
E('ERR_INVALID_TUPLE', '%s must be an iterable %s tuple', TypeError);
817-
E('ERR_INVALID_TYPE_EXTENSION', (extension, flagValue) => {
818-
if (flagValue === 'module')
819-
flagValue = 'module or -m';
820-
return `${extension} is not supported for --type=${flagValue}`;
821-
}, TypeError);
822817
E('ERR_INVALID_TYPE_FLAG',
823818
'Type flag must be one of "module", "commonjs". Received --type=%s',
824819
TypeError);
825-
E('ERR_INVALID_TYPE_IN_PACKAGE_SCOPE', (flagValue, scopeType) => {
826-
if (flagValue === 'module')
827-
flagValue = 'module or -m';
828-
return `Cannot use --type=${flagValue} because nearest parent package.json` +
829-
` includes "type": "${scopeType}"${(scopeType === 'commonjs') ?
830-
' (or lacks the "type" field)' : ''}`;
831-
}, TypeError);
832820
E('ERR_INVALID_URI', 'URI malformed', URIError);
833821
E('ERR_INVALID_URL', 'Invalid URL: %s', TypeError);
834822
E('ERR_INVALID_URL_SCHEME',
@@ -967,6 +955,16 @@ E('ERR_TRANSFORM_ALREADY_TRANSFORMING',
967955
E('ERR_TRANSFORM_WITH_LENGTH_0',
968956
'Calling transform done when writableState.length != 0', Error);
969957
E('ERR_TTY_INIT_FAILED', 'TTY initialization failed', SystemError);
958+
E('ERR_TYPE_MISMATCH', (flagValue, extension, scopeType) => {
959+
if (flagValue === 'module')
960+
flagValue = 'module or -m';
961+
if (extension) // Mismatch between --type and file extension
962+
return `${extension} is not supported for --type=${flagValue}`;
963+
else // Mismatch between --type and package.json "type" field
964+
return `Cannot use --type=${flagValue} because nearest parent ` +
965+
`package.json includes "type": "${scopeType}"${(scopeType === 'commonjs') ?
966+
' (or lacks the "type" field)' : ''}`;
967+
}, TypeError);
970968
E('ERR_UNCAUGHT_EXCEPTION_CAPTURE_ALREADY_SET',
971969
'`process.setupUncaughtExceptionCapture()` was called while a capture ' +
972970
'callback was already active',

lib/internal/modules/cjs/loader.js

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

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

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

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)