reactjs - 路径上的 Eslint 导入/无无关依赖错误

标签 reactjs graphql eslint apollo-client eslintrc

我正在使用 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”的嵌套路径。
当我设置 "import/no-extraneous-dependencies": 0在我的 .eslintrc.js 规则中,它会正常工作。
但是对于适当的解决方案,我假设我可能需要使用 .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 },
    },
  ],
};

最佳答案

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

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

关于reactjs - 路径上的 Eslint 导入/无无关依赖错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67677676/

相关文章:

docker-compose - 如何使用prisma避免java.util.concurrent.ThreadPoolExecutor错误

javascript - "Found @client directives in query but no client resolvers were specified"使用客户端缓存时出现警告

ios - 'SchemaMetadata' 不是 'Self.Schema' 类型的成员类型

webpack - 如何更改 vue.js 应用程序中的 lint 目录?

javascript - 在 React 应用程序中按下刷新或后退按钮时 CSS 丢失

javascript - 使用 Enzyme 测试点击监听器

javascript - 如何在客户端对字符串进行 ESlint?

eslint - 错误解析错误 : Unexpected character '#' in md file

javascript - 单击按钮后如何显示我在文本输入字段中写的内容?

reactjs - 之前时间和之后时间的统计问题