【问题标题】:How I can change eslint resolve settings我如何更改 eslint 解析设置
【发布时间】:2018-10-07 01:21:07
【问题描述】:

我有一个项目,我使用 webpack,eslint。通过 webpack.config 我设置解析 indexIndex 文件。一切正常,除了 eslint 抛出错误import/no-unresolvedimport/extensions,它不知道,除了index,现在它也应该解析Index 文件(import Index from ./components,其中./components 有文件Index.jsx)。我的设置如下。

// .eslintrc
{
  "extends": "airbnb",
  "env": { "browser": true },
  "rules": {
    "no-restricted-syntax": "off",
    "no-continue": "off",
    "no-plusplus": "off",
    "react/prop-types": "off",
    "no-underscore-dangle": "off",
    "no-param-reassign": "off",
    "class-methods-use-this": "off"
  }
}

// package.json
// ...
 "devDependencies": {
    // ...
    "eslint": "^4.19.1",
    "eslint-config-airbnb": "^16.1.0",
    "eslint-plugin-import": "^2.10.0",
    "eslint-plugin-jsx-a11y": "^6.0.3",
    "eslint-plugin-react": "^7.7.0",
    // ...
 }
// ...

【问题讨论】:

  • 如果答案正确,别忘了将答案标记为正确。
  • "react/prop-types": "off" 是个非常糟糕的主意。

标签: javascript reactjs webpack eslint lint


【解决方案1】:

尝试安装 eslint-import-resolver-webpack 并将其添加到您的 .eslintrc:

"settings": {
  "import/resolver": "webpack"
}

或者添加解析依赖的源文件夹;像这样:

settings: {
    'import/resolver': {
        node: {
            paths: [path.resolve(__dirname, 'src')],
        },
    },
}

解析器文档:https://github.com/benmosher/eslint-plugin-import/blob/master/README.md#resolvers

【讨论】:

  • 当我使用 "import/resolver": "webpack" 时,我得到 "TypeError: Cannot read property 'externals' of undefined"
  • @ario 似乎与此错误有关:github.com/benmosher/eslint-plugin-import/issues/1219。检查最新版本是否解决了问题。
  • 此解决方案适用于 src 中的文件。但是 eslint 无法解析根目录下的文件路径。
猜你喜欢
  • 1970-01-01
  • 2020-11-10
  • 2020-09-16
  • 2021-11-01
  • 2022-01-18
  • 2021-04-04
  • 2021-01-24
  • 1970-01-01
  • 2021-03-12
相关资源
最近更新 更多