Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: disable CommonJS support if entry point is ESM #39508

Closed
wants to merge 1 commit into from
Closed
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
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.