question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invoking ESLint via Node API Uses Seemingly Incorrect Path for Configs and Plugins

See original GitHub issue

Tell 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:closed
  • Created 7 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jonathantnealcommented, Dec 12, 2016

The issue may be in lib/config/config-file.js#L312-L315:

function getBaseDir(configFilePath) {

    // calculates the path of the project including ESLint as dependency
    const projectPath = path.resolve(__dirname, "../../../");

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’s node_modules directory.

We can break this down. So, the __dirname will be:

PROJECT/node_modules/eslint/lib/config

Which means "../../../" will take us…

PROJECT/node_modules/eslint/lib
PROJECT/node_modules/eslint
PROJECT/node_modules

I will make a PR to strip lingering node_modules directories. Doing so resolves the issue for me.

1reaction
jonathantnealcommented, Dec 10, 2016
  • ESLint Version: 3.12.0
  • Node Version: 7.2.0
  • npm Version: 4.0.3

I’ve been running into this issue as well. Example:

const cli = new CLIEngine({
  cwd: path.resolve(dir)
});

Within the resolved dir directory, a package.json file is successfully read. Example:

{
  "eslintConfig": {
    "extends": "dev"
  }
}

However, eslint does not resolve the eslint-config-dev module relative to the cwd / package.json, despite that being how it identified the configuration to begin with. Rather, it attempts to resolve the dev configuration from the actual current working directory.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Load plugin when using eslint in node via the API · Issue #4119
I'm having an issue with using the react plugin, and from what I see the problem is the way I use eslint. ......
Read more >
How to fix eslintrc The file does not match your project config?
The error is basically saying the file eslintrc.js itself is both: Found by ESLint, but; Doesn't match the list of files it's supposed...
Read more >
ESLint's new config system, Part 1: Background
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
5 ways to prevent code injection in JavaScript and Node.js
Learn some best practices for keeping your Node.js and JavaScript projects safe from code injection attacks.
Read more >
eslint-config-ash-nazg - npm
However, if you are using more CJS exports, you can override this by adding plugin:node/recommended-script to your extends array (after ash-nazg ) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found