【问题标题】:Eslint import/no-extraneous-dependencies error on path路径上的 Eslint 导入/无外部依赖项错误
【发布时间】:2021-08-13 02:09:15
【问题描述】:

我正在使用 Eslint 设置构建一个 React 应用程序。 在我的应用程序中,我使用 GraphQL @apollo/client 依赖项。

当我尝试做import { setContext } from '@apollo/client/link/context'

我得到一个 eslint 错误

'@apollo/client/link/context' should be listed in the project's dependencies. Run 'npm i -S @apollo/client/link/context' to add it  import/no-extraneous-dependencies

我的 package.json 中确实有“@apollo/client”依赖项。 根据 Apollo 文档,从 '@apollo/client/link/context' 导入是获取 'setContext' 的正确方法。

似乎import/no-extreaneous-dependencies 无法识别来自“@apollo/client”的嵌套路径。

当我在 .eslintrc.js 规则中设置 "import/no-extraneous-dependencies": 0 时,它会正常工作。 但是对于一个正确的解决方案,我假设我可能需要使用 .eslintrc.js 规则设置一些东西,而不是仅仅关闭 eslint 检查。

在这种情况下,我应该为我的 .eslintrc.js 中的规则编写哪些其他设置才能正确解决问题?

我的 package.json:

{
  "name": "my-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@apollo/client": "^3.3.19",
    "@auth0/auth0-react": "^1.4.0",
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "eslint": "^7.26.0",
    "eslint-config-airbnb": "^18.2.1",
    "eslint-plugin-import": "^2.22.1",
    "eslint-plugin-jsx-a11y": "^6.4.1",
    "eslint-plugin-react": "^7.23.2",
    "eslint-plugin-react-hooks": "^4.2.0",
    "graphql": "^15.5.0",
    "react": "^17.0.2",
    "react-big-calendar": "^0.33.3",
    "react-dom": "^17.0.2",
    "react-router-dom": "^5.2.0",
    "react-scripts": "4.0.3",
    "web-vitals": "^1.0.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {}
}

还有我的 .eslintrc.js 文件:

module.exports = {
  env: {
    browser: true,
    es2021: true,
  },
  extends: [
    'plugin:react/recommended',
    'airbnb',
  ],
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
    },
    ecmaVersion: 12,
    sourceType: 'module',
  },
  plugins: [
    'react',
  ],
  rules: {
    'no-console': 1,
    'react/prop-types': 0,
    'no-underscore-dangle': ['error', { allow: ['__raw'] }],
  },
  overrides: [
    {
      files: ['**/*.test.jsx'],
      env: { jest: true },
    },
  ],
};

【问题讨论】:

    标签: reactjs graphql eslint apollo-client eslintrc


    【解决方案1】:

    rxjs/operators 遇到了同样的问题,如 question 67587146 所示。一个解决方案是在.eslintrc.js 中添加核心模块设置的路径。这个解决方案不是很好,但比禁用规则要好。

    settings: {
      'import/core-modules': ['@apollo/client/link/context']
    }
    

    【讨论】:

    • 拯救了我的一天,谢谢!请问为什么不好看?
    • 该规则应该能够解析嵌套包。在解决此问题并且您可以升级之前,此答案可以提供解决方案。
    【解决方案2】:

    其他一些人在最新版本的 eslint-plugin-import 中解决了这个问题,但在 v2.23.4 中对包解析算法进行了修复。

    npm update eslint-plugin-import
    

    Read more about the issue

    【讨论】:

      猜你喜欢
      • 2021-06-20
      • 2022-01-17
      • 2022-07-14
      • 2018-10-29
      • 1970-01-01
      • 2018-01-31
      • 2012-06-11
      • 2015-02-07
      • 2021-09-07
      相关资源
      最近更新 更多