Skip to content

Commit

Permalink
Fix: Load CommonJS .eslintrc.js files within a "type": "module" packa…
Browse files Browse the repository at this point in the history
…ge scope (fixes #12319)
  • Loading branch information
GeoffreyBooth committed Sep 28, 2019
1 parent 29c12f1 commit 8b5b86c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
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

0 comments on commit 8b5b86c

Please sign in to comment.