Skip to content

Commit

Permalink
Try not to use require for modules due to breaking change in Node 22.…
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAnson committed Dec 9, 2024
1 parent b3d64f0 commit 7c16afd
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion markdownlint-cli2.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,18 @@ const importOrRequireResolve = async (dirOrDirs, id, noRequire) => {
const dirs = Array.isArray(dirOrDirs) ? dirOrDirs : [ dirOrDirs ];
const expandId = expandTildePath(id);
const errors = [];
// Try to load via require(...)
try {
return resolveAndRequire(dynamicRequire, expandId, dirs);
const isModule = /\.mjs$/iu.test(expandId);
if (!isModule) {
// Try not to use require for modules due to breaking change in Node 22.12:
// https://github.com/nodejs/node/releases/tag/v22.12.0
return resolveAndRequire(dynamicRequire, expandId, dirs);
}
} catch (error) {
errors.push(error);
}
// Try to load via import(...)
try {
// eslint-disable-next-line n/no-unsupported-features/node-builtins
const isURL = !pathDefault.isAbsolute(expandId) && URL.canParse(expandId);
Expand All @@ -114,6 +121,7 @@ const importOrRequireResolve = async (dirOrDirs, id, noRequire) => {
} catch (error) {
errors.push(error);
}
// Give up
throw new AggregateError(
errors,
`Unable to require or import module '${id}'.`
Expand Down

0 comments on commit 7c16afd

Please sign in to comment.