Invoking ESLint via Node API Uses Seemingly Incorrect Path for Configs and Plugins
See original GitHub issueTell us about your environment
- ESLint Version: 3.11.1
- Node Version: 7.2
- npm Version: 3.10.9
What parser (default, Babel-ESLint, etc.) are you using? default
Please show your full configuration:
{
"extends": "@example/eslint-config-example
}
What did you do? Please include the actual source code causing the issue.
const cliEngine = new CLIEngine({
useEslintrc: true,
cwd: process.cwd()
});
const report = cliEngine.executeOnFiles(files);
What did you expect to happen?
I have a utility that is globally installed and invoked via the command line. It has a dependency on eslint
, but should execute the rules and plugins specified in the cwd
’s .eslintrc
. That mostly seems to work, but when it attempts to process the extends, the path received from getBaseDir
seems incorrect. In particular, it tries to search relative to the eslint
in the globally installed tool rather than relative to the .eslintrc
, which means that it misses the dependencies installed by the project.
Basically, the dependencies I’ve got are:
SomeProject:
devDependencies:
eslint-config-mine:
dependencies:
eslint-config-otherTeam,
eslint-plugin-mine,
MyTool:
dependencies:
eslint
What actually happened? Please include the actual, raw output from ESLint.
/Users/wstamper/mytool/node_modules/eslint/lib/config/config-file.js:402
throw e;
^
Error: Cannot find module 'eslint-config-mine'
Referenced from: /Users/wstamper/github/someproject/.eslintrc.json
at ModuleResolver.resolve (/Users/wstamper/mytool/node_modules/eslint/lib/util/module-resolver.js:74:19)
at resolve (/Users/wstamper/mytool/node_modules/eslint/lib/config/config-file.js:479:33)
at load (/Users/wstamper/mytool/node_modules/eslint/lib/config/config-file.js:496:26)
at configExtends.reduceRight (/Users/wstamper/mytool/node_modules/eslint/lib/config/config-file.js:392:36)
at Array.reduceRight (native)
at applyExtends (/Users/wstamper/mytool/node_modules/eslint/lib/config/config-file.js:363:28)
at Object.load (/Users/wstamper/mytool/node_modules/eslint/lib/config/config-file.js:530:22)
at loadConfig (/Users/wstamper/mytool/node_modules/eslint/lib/config.js:63:33)
at getLocalConfig (/Users/wstamper/mytool/node_modules/eslint/lib/config.js:130:29)
at Config.getConfig (/Users/wstamper/mytool/node_modules/eslint/lib/config.js:256:22)
at processText (/Users/wstamper/mytool/node_modules/eslint/lib/cli-engine.js:224:33)
at processFile (/Users/wstamper/mytool/node_modules/eslint/lib/cli-engine.js:303:18)
at executeOnFile (/Users/wstamper/mytool/node_modules/eslint/lib/cli-engine.js:672:25)
at fileList.forEach.fileInfo (/Users/wstamper/mytool/node_modules/eslint/lib/cli-engine.js:710:13)
at Array.forEach (native)
at CLIEngine.executeOnFiles (/Users/wstamper/mytool/node_modules/eslint/lib/cli-engine.js:709:18)
Issue Analytics
- State:
- Created 7 years ago
- Comments:9 (4 by maintainers)
The issue may be in lib/config/config-file.js#L312-L315:
The
projectPath
that gets calculated here does not seem to be “the path of the project including eslint as a dependency”, but is actually the project’snode_modules
directory.We can break this down. So, the
__dirname
will be:Which means
"../../../"
will take us…I will make a PR to strip lingering
node_modules
directories. Doing so resolves the issue for me.I’ve been running into this issue as well. Example:
Within the resolved
dir
directory, apackage.json
file is successfully read. Example:However, eslint does not resolve the
eslint-config-dev
module relative to thecwd
/package.json
, despite that being how it identified the configuration to begin with. Rather, it attempts to resolve thedev
configuration from the actual current working directory.