简体   繁体   English

与 TS 反应,eslint 错误导入/未解决

[英]React with TS, eslint error import/no-unresolved

  • I have next ESLINT issue:我有下一个 ESLINT 问题:

    Unable to resolve path to module 'lib-name' import/no-unresolved无法解析模块“lib-name”导入/未解决的路径

  • App tech is React with Typescript应用技术是 React with Typescript

  • On running eslint got a bounch above error在运行 eslint 时出现了以上错误

  • Tried a lot of different solution and still has the same problem尝试了很多不同的解决方案,仍然有同样的问题

this is .eslintrc.js file这是.eslintrc.js文件

const sharedRules = {
  // import
  'import/prefer-default-export': 'off',
  'import/no-extraneous-dependencies': 'off', // This is useful for lodash imports like "import get from lodash/get"
  'import/extensions': [
    'error',
    'ignorePackages',
    {
      js: 'never',
      jsx: 'never',
      ts: 'never',
      tsx: 'never',
    },
  ],
  // js
  'curly': 2,
  'no-use-before-define': 'off', // must disable base rule to use proper typescript-eslint rule
  'camelcase': 'off', // This is unavoidable in data coming from the BE
  'max-classes-per-file': 'off',
  'no-octal': 'off',
  'no-shadow': 'off',
  'no-underscore-dangle': 'off',
  // react
  'react-hooks/exhaustive-deps': 'warn',
  'react/jsx-filename-extension': 'off',
  'react/jsx-one-expression-per-line': 'off',
  'react/jsx-props-no-spreading': 'off',
  'react/prop-types': 'off',
  'react/no-unused-prop-types': 'off',
  'react/require-default-props': 'off',
  'react/react-in-jsx-scope': 'off', // https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html
  // a11y
  'jsx-a11y/anchor-is-valid': 'off',
  'jsx-a11y/label-has-associated-control': 'off', // Just a hassle, you can nest inputs inside labels and it works fine. Check checkbox component for example.
  'jsx-a11y/control-has-associated-label': 'off',
  'jsx-a11y/no-noninteractive-tabindex': 'off',
  'jsx-a11y/click-events-have-key-events': 'off',
  'jsx-a11y/no-static-element-interactions': 'off',
  'import/no-unresolved': [2, { caseSensitive: false }],
};

const typeScriptRules = {
  '@typescript-eslint/no-explicit-any': 'warn',
  '@typescript-eslint/ban-ts-comment': 'warn',
  '@typescript-eslint/explicit-function-return-type': 'off',
  '@typescript-eslint/no-use-before-define': ['error'],
  '@typescript-eslint/no-non-null-assertion': 'off',
  ...sharedRules,
};

module.exports = {
  env: {
    browser: true,
    es6: true,
    jest: true,
  },
  extends: ['airbnb', 'airbnb/hooks', 'prettier'],
  globals: {
    createMockHttpRequest: true,
    createStore: true,
    mockAjaxRequest: true,
  },
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
    },
    ecmaVersion: 11,
    sourceType: 'module',
  },
  plugins: ['@typescript-eslint', 'eslint-plugin-prettier'],
  rules: { ...sharedRules },
  overrides: [
    {
      extends: [
        'airbnb',
        'airbnb/hooks',
        'plugin:@typescript-eslint/eslint-recommended',
        'plugin:@typescript-eslint/recommended',
        'prettier',
        'prettier/@typescript-eslint',
        'prettier/react',
      ],
      files: ['**/*.ts', '**/*.tsx'],
      rules: { ...typeScriptRules },
      settings: {
        'import/resolver': {
          node: {
            extensions: ['.js', '.jsx', '.ts', '.tsx'],
          },
        },
      },
    },
  ],
};

-- I have also next file in repo eslintrc.json -- 我在 repo eslintrc.json中还有下一个文件

{
  "root": true,
  "plugins": ["@nrwl/nx", "react", "react-hooks"],
  "overrides": [
    {
      "files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
      "rules": {
        "@nrwl/nx/enforce-module-boundaries": [
          "error",
          {
            "enforceBuildableLibDependency": true,
            "allow": [],
            "depConstraints": [
              {
                "sourceTag": "*",
                "onlyDependOnLibsWithTags": ["*"]
              }
            ]
          }
        ]
      },
    },
    {
      "files": ["*.ts", "*.tsx"],
      "extends": ["plugin:@nrwl/nx/typescript"],
      "rules": {}
    },
    {
      "files": ["*.js", "*.jsx"],
      "extends": ["plugin:@nrwl/nx/javascript"],
      "rules": {}
    }
  ]
}

Any help is welcome:!欢迎任何帮助:! :) :)

I fixed this one on my Nx project by adding this to the .eslintrc.json at the root of the project:我通过将其添加到项目根目录中的.eslintrc.json来修复我的 Nx 项目中的这个问题:

"settings": {
  "import/parsers": {
    "@typescript-eslint/parser": [".ts", ".tsx"]
  },
  "import/resolver": {
    "typescript": {
      "project": ["tsconfig.base.json"]
    },
    "node": {
      "project": ["tsconfig.base.json"]
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 eslint 在声明文件上抛出 import/no-unresolved 错误 - eslint throwing import/no-unresolved error on declaration files 如何从导入“react”中删除未解决的 ESlint 错误 - How to remove ESlint error no-unresolved from importing 'react' 如何在 ESLint 中为导入文件解析“import/no-unresolved”! 对于 javascript 和 nodejs - How to resolve "import/no-unresolved" in ESLint for import files! for javascript and nodejs 如何在Webpack 4中使用eslint的安装来解决导入/无法解决的问题 - how to resolve import/no-unresolved with setup of eslint in webpack 4 无法在 eslint 中解析模块路径(导入/未解析) - Unable to resolve path to module (import/no-unresolved) in eslint JavaScript on Next.js 和 React 导入错误“无法解析模块'../utils/makeId'.eslintimport/no-unresolved 的路径” - JavaScript on Next.js and React import error "Unable to resolve path to module '../utils/makeId'.eslintimport/no-unresolved" 带有 Eslint 的 Vite.js 上的 Vue 3 — 无法解析模块 eslint 的路径(导入/未解决) - Vue 3 on Vite.js with Eslint — Unable to resolve path to module eslint(import/no-unresolved) 规则 'import/no-unresolved' 的生成的故事条目.js 错误 - generated-stories-entry.js error for rule 'import/no-unresolved' 错误无法解析模块“html-webpack-plugin”导入的路径/未解决 - error Unable to resolve path to module 'html-webpack-plugin' import/no-unresolved 样式化组件导入和 eslint 出错 - Error with styled component import and eslint
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM