【问题标题】:eslint complaining about resolving pathseslint 抱怨解决路径
【发布时间】:2021-01-16 14:21:31
【问题描述】:

我不断收到 eslint 错误 import/no-unresolved 并且不确定如何修复它。我知道这与 babel 有关,但我不确定如何让 babel 和 eslint 玩得很好。

我的 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,
      },
    ],
  ],
};

…和我的 .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',
      },
    },
  },
};

每种类型的导入都会出现此问题:

有什么建议吗?

编辑

我已经使用我在这个项目中使用的相同 eslint 设置创建了一个存储库。它显示的错误与我看到的相同。

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

【问题讨论】:

  • 您好 Brandon,可能存在以下情况 - 您尝试从文件系统/模块导入的方式和内容。如果您可以发布出现错误的用法 - 可能会有用。您可以尝试的一件事是在 eslintrc 文件中添加道具以忽略大小写敏感性“规则”:{“import/no-unresolved”:[2,{“caseSensitive”:false}]}
  • 添加了显示所有导入的 eslint 错误的屏幕截图。
  • 第 1 步:开始将其减少到您的 eslint 可能为 2 或 3 行的位置,但您仍然会遇到错误。然后开始减少你的 babel 配置,直到它只有几行。然后将您的实际测试用例减少到在单个文件上运行该配置。现在,您有一个出色的 Stackoverflow minimal reproducible example,您已经完成了 可以做的所有工作,以深入了解可能出现的问题,其他人可以立即为您处理。如果您在形成 MCVE 时还没有找到解决方案。
  • 虽然我非常怀疑这是因为您说您希望 ESLint 将您的代码验证为 ecmaVersion6 而不是 2018。 6 没有模块支持。

标签: javascript babeljs eslint


【解决方案1】:

您至少必须更新您的 eslint 配置以表明您想要 ES2018(或更高版本)支持,因为您正在使用模块导入验证现代 JS:

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

因为从 https://eslint.org/docs/user-guide/configuring#specifying-parser-options 我们知道 ecmaVersion: 6 对应于要求 ESLint 验证您的代码为 ES2015,它早于 ES 模块,因此将正确地将任何 import ... from "..." 语句标记为错误。

我用 大量 精简的 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
  }
};

这很好用,所以:开始备份它直到它坏了,这样你就可以更好地了解真正需要解决的问题。

【讨论】:

  • 这似乎是答案,但不幸的是没有解决问题。
  • 那么你仍然需要创建一个minimal reproducible example 并用它更新你的初始帖子。
  • 我添加了一个小仓库的链接。
  • 请记住,Stackoverflow 的想法是问题包含所有详细信息 - 指向现场演示或 repos 的链接很好,但只有 除了 在您的发布,因为外部链接变成了 404。 (明天或 10 年后发现您的问题的未来访问者应该仍然可以将其用作参考,无论链接是否已失效)
  • 你明白了。非常感谢您的帮助和建议。
猜你喜欢
  • 1970-01-01
  • 2021-11-09
  • 2014-09-20
  • 2016-08-31
  • 1970-01-01
  • 2016-07-02
  • 2023-03-18
  • 2014-08-14
  • 1970-01-01
相关资源
最近更新 更多