【问题标题】:Why does ESLint throw an error while using export/import statement on Node.js 12.13.0?为什么在 Node.js 12.13.0 上使用 export/import 语句时 ESLint 会抛出错误?
【发布时间】:2020-03-17 02:10:25
【问题描述】:

我有一个基于 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": []
    }]
  }
}
`

【问题讨论】:

  • 您使用.js 还是.mjs?因为 Node.js 原生支持带有特性标志和新文件扩展名 *.mjs 的 ES 模块。
  • 我正在使用.js
  • 您的.eslintrc.js 文件看起来如何?
  • @MickaelB 已在问题详情中添加文件内容

标签: node.js npm eslint serverless


【解决方案1】:

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

在我的情况下,我通过将引擎定义添加到 package.json 来解决它

"engines": {
    "node": ">=12.13.0"
}

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

【讨论】:

    【解决方案2】:

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

    *.mjs 你使用 ES6 导入/导出

    *.js 或 *.cjs 你使用 commonjs

    如果您认为 ECMAScript 模块导入/导出仍处于试验阶段,我会说 ESLint 是完全正确的。

    您可能希望覆盖 EsLint 规则以使其正常工作。 首先考虑您提到的规则已过时,因此您可能希望使用新规则 => https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/no-unsupported-features.md

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

    无论如何……

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

    【讨论】:

      猜你喜欢
      • 2017-03-08
      • 2019-08-28
      • 2017-12-18
      • 2016-03-30
      • 1970-01-01
      • 2021-03-13
      • 1970-01-01
      • 2020-12-09
      • 1970-01-01
      相关资源
      最近更新 更多