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

Fix: Load CommonJS .eslintrc.js files within a "type": "module" package scope (fixes #12319) #12333

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/cli-engine/config-array-factory.js
Expand Up @@ -36,6 +36,7 @@
const fs = require("fs");
const path = require("path");
const importFresh = require("import-fresh");
const requireFromString = require("require-from-string");
const stripComments = require("strip-json-comments");
const { validateConfigSchema } = require("../shared/config-validator");
const naming = require("../shared/naming");
Expand Down Expand Up @@ -188,6 +189,19 @@ function loadJSConfigFile(filePath) {
try {
return importFresh(filePath);
} catch (e) {
if (e.code === "ERR_REQUIRE_ESM") {

/*
* The JS config file is a CommonJS .js file within a `"type": "module"``
* package scope. Node errors when trying to require such a file,
* so bypass Node’s check. There’s no need for `importFresh`, because
* this method never adds the loaded .js file into `require.cache`.
*/
const content = readFile(filePath);

return requireFromString(content, filePath, { appendPaths: path.dirname(filePath) });
}

debug(`Error reading JavaScript file: ${filePath}`);
e.message = `Cannot read config file: ${filePath}\nError: ${e.message}`;
throw e;
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -74,6 +74,7 @@
"optionator": "^0.8.2",
"progress": "^2.0.0",
"regexpp": "^2.0.1",
"require-from-string": "^2.0.2",
"semver": "^6.1.2",
"strip-ansi": "^5.2.0",
"strip-json-comments": "^3.0.1",
Expand Down