2

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

解析错误:“import”和“export”可能只出现在“sourceType:module”中

我将.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"]
  }
}

使用以下 devDependencies:

  "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"
  },

并设置我的引擎:

  "engines": {
    "node": "^10"
  }

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

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

我收到以下警报:

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

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

4

4 回答 4

2

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

{
  "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"
  }
}

这里的关键是规则'node/no-unsupported-features/es-syntax'。我在这里所做的只是覆盖由“插件:节点/推荐”添加的这条规则,以允许 ES 模块语法。

于 2021-02-27T08:36:57.287 回答
2

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

于 2021-01-22T18:56:17.677 回答
-1

检查您的 .eslintrc 文件。可能是因为 node/recommended 插件。 "extends": ["plugin:node/recommended"]

于 2021-03-04T22:54:37.477 回答
-4

我只是有同样的问题,删除 "plugin:node/recommended" 为我做了。希望这可以帮助!

于 2019-11-20T10:52:13.257 回答