gpt4 book ai didi

node.js - 为什么在 Node.js 12.13.0 上使用导出/导入语句时 ESLint 会抛出错误?

转载 作者:行者123 更新时间:2023-12-04 17:31:45 24 4
gpt4 key购买 nike

我有一个基于 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

我已经通过我的项目和本地的 Node 版本进行了双重验证。两者相同 (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 使用 Common.js 样式进行导入/导出。在新版本的 Node.js 中,您可以使用不同的扩展或命令行中的 --experimental-modules 选项。

*.mjs 你使用 ES6 import/export

*.js 或 *.cjs 你用commonjs

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

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

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

无论如何...

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

关于node.js - 为什么在 Node.js 12.13.0 上使用导出/导入语句时 ESLint 会抛出错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58969925/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com