我在 ESLint 文件中做错了什么,因此导入不会收到警报?

Dᴀʀ*_*ᴅᴇʀ 5 alert eslint babeljs eslintrc babel-eslint

在阅读了有关我的问题的一些问答后:

解析错误:“导入”和“导出”可能仅与“sourceType:模块”一起出现

我将.eslintrc.json写入:

{
  "extends": ["airbnb", "prettier", "plugin:node/recommended"],
  "plugins": ["prettier"],
  "env": {
    "node": true,
    "es6": true,
    "browser": true
  },
  "parser": "babel-eslint",
  "parserOptions": {
    "ecmaVersion": 11,
    "sourceType": "module",
    "allowImportExportEverywhere": true
  },
  "rules": {
    "prettier/prettier": "error",
    "spaced-comment": "off",
    "no-console": "warn",
    "consistent-return": "off",
    "func-names": "off",
    "object-shorthand": "off",
    "no-process-exit": "off",
    "no-param-reassign": "off",
    "no-return-await": "off",
    "no-underscore-dangle": "off",
    "class-methods-use-this": "off",
    "prefer-destructuring": ["error", { "object": true, "array": false }],
    "no-unused-vars": ["error", { "argsIgnorePattern": "req|res|next|val" }],
    "semi": [2, "never"],
    "object-curly-spacing": [2, "always"]
  }
}
Run Code Online (Sandbox Code Playgroud)

使用以下的 devDependency:

  "devDependencies": {
    "babel-eslint": "^10.0.3",
    "eslint": "^6.4.0",
    "eslint-config-airbnb": "^18.0.1",
    "eslint-config-prettier": "^6.3.0",
    "eslint-plugin-import": "^2.18.2",
    "eslint-plugin-jsx-a11y": "^6.2.3",
    "eslint-plugin-node": "^10.0.0",
    "eslint-plugin-prettier": "^3.1.1",
    "eslint-plugin-react": "^7.14.3",
    "eslint-plugin-react-hooks": "^1.7.0",
    "nodemon": "^1.19.3",
    "prettier": "^1.18.2"
  },
Run Code Online (Sandbox Code Playgroud)

并设置我的引擎:

  "engines": {
    "node": "^10"
  }
Run Code Online (Sandbox Code Playgroud)

当我用 Babel 编写测试模块时:

export default class Search {
  constructor() {
    alert('search test')
  }
}

Run Code Online (Sandbox Code Playgroud)

我收到以下警报:

尚不支持导入和导出声明。eslint(node/no-unsupported-features/es-syntax)

为什么我的设置不起作用?我可以在.eslintignoremodules/文件中写入忽略,但我想知道为什么收到此警报以及如何正确解决它?

小智 7

可能有更好的方法来做到这一点,但这对我有用。

{
  "extends": ["airbnb", "prettier", "plugin:node/recommended"],
  "plugins": ["prettier"],
  "rules": {
    "prettier/prettier": "error",
    "spaced-comment": "off",
    "no-console": "warn",
    "consistent-return": "off",
    "func-names": "off",
    "object-shorthand": "off",
    "no-process-exit": "off",
    "no-param-reassign": "off",
    "no-return-await": "off",
    "no-underscore-dangle": "off",
    "class-methods-use-this": "off",
    "prefer-destructuring": ["error", { "object": true, "array": false }],
    "no-unused-vars": ["error", { "argsIgnorePattern": "req|res|next|val" }],
    "node/no-unsupported-features/es-syntax": [
      "error",
      {
        "version": ">=13.0.0",
        "ignores": ["modules"]
      }
    ],
    "import/extensions": [
      "error",
      {
        "js": "ignorePackages"
      }
    ]
  },
  "parserOptions": {
    "sourceType": "module"
  }
}
Run Code Online (Sandbox Code Playgroud)

这里的关键是规则“node/no-unsupported-features/es-syntax”。我在这里所做的就是覆盖“plugin:node/recommended”添加的这条规则,以允许 ES 模块语法。


Boo*_*gie 5

"type":"module"正如@joan Gil所说,尝试在你的上使用package.json(只要你使用的节点> 12)


小智 -6

我刚刚遇到了同样的问题,删除 "plugin:node/recommended" 对我来说是这样。希望这可以帮助!

  • 这不应该是公认的答案。删除该插件实际上会将其规则从 ESLint 中排除。基本上就是根本不安装,不推荐。我建议查看插件文档 https://github.com/mysticatea/eslint-plugin-node。对于这种情况,我认为 package.json 中缺少“type”:“module”。将“plugin:node/recommended-module”添加到插件列表中也可能有效 (5认同)