Skip to content

Commit

Permalink
module: disable CommonJS support if entry point is ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Jul 24, 2021
1 parent 32723ae commit 914a363
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
6 changes: 5 additions & 1 deletion lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,19 @@ const {
StringPrototypeSlice,
StringPrototypeSplit,
StringPrototypeStartsWith,
Symbol,
} = primordials;

const kContainsESMSyntax = Symbol('contains ESM syntax');

// Map used to store CJS parsing data.
const cjsParseCache = new SafeWeakMap();

// Set first due to cycle with ESM loader functions.
module.exports = {
wrapSafe, Module, toRealPath, readPackageScope, cjsParseCache,
get hasLoadedAnyUserCJSModule() { return hasLoadedAnyUserCJSModule; }
get hasLoadedAnyUserCJSModule() { return hasLoadedAnyUserCJSModule; },
kContainsESMSyntax,
};

const { NativeModule } = require('internal/bootstrap/loaders');
Expand Down
4 changes: 3 additions & 1 deletion lib/internal/modules/esm/translators.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ const {
} = require('internal/modules/cjs/helpers');
const {
Module: CJSModule,
cjsParseCache
cjsParseCache,
kContainsESMSyntax,
} = require('internal/modules/cjs/loader');
const internalURLModule = require('internal/url');
const { defaultGetSource } = require(
Expand Down Expand Up @@ -171,6 +172,7 @@ function enrichCJSError(err) {
'To load an ES module, set "type": "module" in the package.json or use ' +
'the .mjs extension.'
);
err[kContainsESMSyntax] = true;
}
}

Expand Down
23 changes: 20 additions & 3 deletions lib/internal/modules/run_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const {
StringPrototypeEndsWith,
} = primordials;
const CJSLoader = require('internal/modules/cjs/loader');
const { Module, toRealPath, readPackageScope } = CJSLoader;
const { Module, kContainsESMSyntax, toRealPath, readPackageScope } = CJSLoader;
const { getOptionValue } = require('internal/options');
const path = require('path');

Expand Down Expand Up @@ -75,8 +75,25 @@ function executeUserEntryPoint(main = process.argv[1]) {
if (useESMLoader) {
runMainESM(resolvedMain || main);
} else {
// Module._load is the monkey-patchable CJS module loader.
Module._load(main, null, true);
try {
// Module._load is the monkey-patchable CJS module loader.
Module._load(main, null, true);
} catch (err) {
if (err?.[kContainsESMSyntax] && !readPackageScope(resolvedMain)) {
const { emitWarning } = require('internal/process/warning');
emitWarning(`No package.json detected, attempting to load "${main}" with CommonJS support disabled.`);
const esmLoader = require('internal/process/esm_loader');
esmLoader.ESMLoader._getFormat = (url, _, defaultGetFormat) => {
const defaultFormat = defaultGetFormat(url);
if (defaultFormat.format === 'commonjs')
defaultFormat.format = 'module';
return defaultFormat;
};
runMainESM(resolvedMain || main);
} else {
throw err;
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(node:*) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
(node:*) Warning: No package.json detected, attempting to load "*/test/message/esm_syntax_in_entry_point_without_package.json.js" with CommonJS support disabled.

0 comments on commit 914a363

Please sign in to comment.