简体   繁体   English

eslint 抱怨解决路径

[英]eslint complaining about resolving paths

I keep receiving the eslint error import/no-unresolved and am unsure as to how I fix it.我一直收到 eslint 错误import/no-unresolved并且不确定如何修复它。 I know this is related to babel, but I'm not sure how to get babel and eslint to play nice.我知道这与 babel 相关,但我不确定如何让 babel 和 eslint 发挥良好的作用。

My babel.config.js file:我的 babel.config.js 文件:

module.exports = {
  presets: [['@babel/preset-env', { targets: { node: 'current' } }], '@babel/preset-react'],
  plugins: [
    'babel-plugin-macros',
    '@babel/plugin-syntax-dynamic-import',
    '@babel/plugin-transform-destructuring',
    [
      '@babel/plugin-proposal-class-properties',
      {
        loose: true,
      },
    ],
    [
      '@babel/plugin-proposal-object-rest-spread',
      {
        useBuiltIns: true,
      },
    ],
    [
      '@babel/plugin-transform-runtime',
      {
        helpers: false,
        regenerator: true,
      },
    ],
    [
      '@babel/plugin-transform-regenerator',
      {
        async: false,
      },
    ],
  ],
};

… and my .eslintrc.js file ...和我的 .eslintrc.js 文件

module.exports = {
  parser: 'babel-eslint',
  extends: 'airbnb',
  env: {
    browser: true,
    node: true,
    jest: true,
    es6: true,
  },
  plugins: ['react', 'jsx-a11y'],
  parserOptions: {
    ecmaVersion: 6,
    sourceType: 'module',
    ecmaFeatures: {
      jsx: true,
    },
  },
  rules: {
    'object-curly-spacing': 2,
    'arrow-parens': ['error', 'as-needed'],
    'arrow-body-style': [2, 'as-needed'],
    'comma-dangle': [2, 'always-multiline'],
    'import/imports-first': 0,
    'import/newline-after-import': 0,
    'import/no-dynamic-require': 0,
    'import/no-extraneous-dependencies': 0,
    'import/no-named-as-default': 0,
    'import/no-unresolved': 2,
    'import/prefer-default-export': 0,
    indent: [
      2,
      2,
      {
        SwitchCase: 1,
      },
    ],
    'space-before-function-paren': 0,
    'jsx-a11y/aria-props': 2,
    'jsx-a11y/heading-has-content': 0,
    'jsx-a11y/label-has-associated-control': 2,
    'jsx-a11y/mouse-events-have-key-events': 2,
    'jsx-a11y/no-autofocus': 0,
    'jsx-a11y/role-has-required-aria-props': 2,
    'jsx-a11y/role-supports-aria-props': 2,
    'max-len': 0,
    'newline-per-chained-call': 0,
    'no-confusing-arrow': 0,
    'no-console': 1,
    'no-use-before-define': 0,
    'prefer-template': 2,
    'class-methods-use-this': 0,
    'react/forbid-prop-types': 0,
    'react/jsx-first-prop-new-line': [2, 'multiline'],
    'react/jsx-filename-extension': 0,
    'react/prefer-stateless-function': 0,
    'react/jsx-no-target-blank': 0,
    'react/require-extension': 0,
    'react/prop-types': 0,
    'react/self-closing-comp': 0,
    'require-yield': 0,
    'import/no-webpack-loader-syntax': 0,
    semi: ['error', 'always'],
  },
  settings: {
    'import/resolver': {
      webpack: {
        config: 'config/webpack/development.js',
      },
    },
  },
};

The issue occurs for every type of import:每种类型的导入都会出现此问题: eslint 错误截图

Any suggestions?有什么建议?

EDIT编辑

I've created a repository using the same eslint setup as I'm using in this project.我已经使用与我在这个项目中使用的相同的 eslint 设置创建了一个存储库。 It's showing the same error I'm seeing.它显示了我所看到的相同错误。

https://github.com/brandondurham/eslint-test https://github.com/brandondurham/eslint-test

You'll have to at the very least update your eslint config to say you want ES2018 (or newer) support, since you're validating modern JS with module imports:您至少必须更新您的 eslint 配置以表示您想要 ES2018(或更新的)支持,因为您正在使用模块导入验证现代 JS:

module.exports = {
  ...
  parserOptions: {
    ecmaVersion: 2018,
    sourceType: 'module',
    ecmaFeatures: {
      jsx: true,
    },
  },
  ...
};

Because from https://eslint.org/docs/user-guide/configuring#specifying-parser-options we know that ecmaVersion: 6 corresponds to asking ESLint to validate your code as ES2015, which predates ES modules, and so will correctly flag any import ... from "..." statement as an error.因为从https://eslint.org/docs/user-guide/configuring#specifying-parser-options我们知道ecmaVersion: 6对应于要求 ESLint 将您的代码验证为 ES2015,它早于 ES 模块,因此将正确标记任何import ... from "..."语句作为错误。

I tried your github repo with a heavily reduced ESlint config:我用大大减少的 ESlint 配置尝试了你的 github 存储库:

module.exports = {
  parser: 'babel-eslint',
  extends: 'airbnb',
  env: {
    browser: true,
    node: true,
    jest: true,
    es6: true
  },
  plugins: ['react'],
  parserOptions: {
    ecmaVersion: 2018,
    sourceType: 'module',
    ecmaFeatures: {
      jsx: true,
    },
  },
  rules: {
    'no-unused-vars': 0
  }
};

And that works just fine, so: start building it back up until it breaks, so you have a better idea of what really needs addressing.这工作得很好,所以:开始重新构建它直到它损坏,这样你就可以更好地了解真正需要解决的问题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM