为什么在 Node.js 12.13.0 上使用 export/import 语句时 ESLint 会抛出错误?

Why does ESLint throw an error while using export/import statement on Node.js 12.13.0?

我有一个基于 Node.js、DynamoDB 和其他 AWS 服务构建的项目,部署在无服务器架构上。我还安装了 ESLint 包。

我收到以下错误:

ESLint: Import and export declarations are not supported yet on Node 8.0.0. (node/no-unsupported-features)

以下是我的项目的详细信息:

Node version: 12.13.0
NPM version: 6.12.0
Serverless version: 1.40.0
ESLint: 6.8.0

我已经通过我的项目和本地的节点版本进行了双重验证。两者相同 (12.13.0)。

我可以使用 async/await 但每当我尝试使用 import/export 时,它都会给我错误。

以下是我的 .eslintrc 文件:

{
  "extends" : [
    "eslint:recommended",
    "plugin:node/recommended"
  ],
  "plugins": [
    "promise",
    "node"
  ],
  "env"     : {
    "browser" : false,
    "node": true,
    "es6": true
  },
  "parserOptions": {
    "ecmaVersion": 9,
    "sourceType": "module",
    "impliedStrict": false
  },
  "globals" : {
  },
  "rules": {
    "no-console": 0,
    "no-unused-vars": ["error", { "vars": "all", "args": "none", "ignoreRestSiblings": false }],
    "node/no-unpublished-require": [
      "error",
      {
        "allowModules": [
          "aws-sdk"
        ]
      }
    ],
    "node/no-unsupported-features": ["error", {
      "version": 8,
      "ignores": []
    }]
  }
}
`

Node.js 默认为 import/export 使用 Common.js 样式。 在 Node.js 的新版本中,您可以使用不同的扩展名或命令行中的 --experimental-modules 选项。

*.mjs 你用的是 ES6 import/export

*.js 或 *.cjs 你用commonjs

如果您认为 ECMAScript 模块 import/export 仍处于实验阶段,我会说 ESLint 是非常正确的。

您可能想要覆盖 EsLint 规则以使其与之配合使用。 首先考虑你提到的规则已经过时,所以你可能想使用新的 => https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/no-unsupported-features.md

因为这些规则已过时,我建议您查看您的 eslint 及其外部规则集版本(如果您使用它们),然后重试。

无论如何...

Check/create 项目根目录中的 .eslintrc.json 文件并覆盖您可能想要更改的项目规则。

刚刚偶然发现了一个类似的错误。

在我的例子中,我通过将引擎定义添加到 package.json

来解决它
"engines": {
    "node": ">=12.13.0"
}

我不确定这是否能解决您的问题,但可以找到一个非常相似的问题