我正在尝试获取本地eslintrc(在项目根目录中)以扩展全局eslintrc(位于~/.eslintrc)。

这是本地eslintrc

{
  "extends": "~/.eslintrc"
}


这是运行eslint foo.js时遇到的错误:

Cannot read config file: /Users/cbickel1/dev/learning/webpack-demo/~/.eslintrc
Error: ENOENT: no such file or directory, open '/Users/cbickel1/dev/learning/webpack-demo/~/.eslintrc'
Referenced from: /Users/cbickel1/dev/learning/webpack-demo/.eslintrc
Error: Cannot read config file: /Users/cbickel1/dev/learning/webpack-demo/~/.eslintrc
Error: ENOENT: no such file or directory, open '/Users/cbickel1/dev/learning/webpack-demo/~/.eslintrc'
Referenced from: /Users/cbickel1/dev/learning/webpack-demo/.eslintrc
    at Object.fs.openSync (fs.js:558:18)
    at Object.fs.readFileSync (fs.js:468:33)
    at readFile (/Users/cbickel1/.nvm/versions/node/v7.4.0/lib/node_modules/eslint/lib/config/config-file.js:70:24)
    at loadLegacyConfigFile (/Users/cbickel1/.nvm/versions/node/v7.4.0/lib/node_modules/eslint/lib/config/config-file.js:142:44)
    at loadConfigFile (/Users/cbickel1/.nvm/versions/node/v7.4.0/lib/node_modules/eslint/lib/config/config-file.js:222:22)
    at load (/Users/cbickel1/.nvm/versions/node/v7.4.0/lib/node_modules/eslint/lib/config/config-file.js:505:18)
    at configExtends.reduceRight (/Users/cbickel1/.nvm/versions/node/v7.4.0/lib/node_modules/eslint/lib/config/config-file.js:391:36)
    at Array.reduceRight (native)
    at applyExtends (/Users/cbickel1/.nvm/versions/node/v7.4.0/lib/node_modules/eslint/lib/config/config-file.js:362:28)
    at Object.load (/Users/cbickel1/.nvm/versions/node/v7.4.0/lib/node_modules/eslint/lib/config/config-file.js:536:22)


看起来ESLint没有正确解析给定的绝对路径。我该怎么办?

最佳答案

eslint从v4.7.0版本开始不支持项目根目录。

查看源代码,您需要提供.eslintrc文件的绝对或相对路径

资源
https://github.com/eslint/eslint/blob/v4.7.0/lib/config/config-file.js#L413

例如

绝对(Windows)

{ "extends": "c:/git/project/.eslintrc"}

绝对(Mac / Linux)

{ "extends": "/Users/username/git/project/.eslintrc"}

相对的

{ "extends": "../../../.eslintrc"}

10-07 12:52