node.js - 为什么在 Node.js 12.13.0 上使用 export/import 语句时 ESLint 会抛出错误?
问题描述
我有一个基于 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 默认使用 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
文件并覆盖您可能想要更改的项目规则。
推荐阅读
- kentico - Kentico Hotfix 实用程序似乎没有做任何事情
- javascript - Javascript 语法: const {Observables} = rxjs
- python - pip install ecapture 无法构建 scikit-image
- javascript - 单独的驼峰式字符串
- sql - PL SQL:Oracle 数据库错误 907:ORA-00907:缺少右括号
- google-cloud-platform - Azure Active Directory 单点登录 (SSO) 与 Google Cloud (G Suite) 的集成
- javascript - 监听用户快照发送事件(或等效)
- assembly - 如何计算偏移量以使用地址 x3100 加载 R1?
- flutter - 如何在使用 bloc 滚动时隐藏底部小部件
- performance - 如果我在 IntelliJ 中运行两个不同的类,它们会共享资源(如 CPU)吗?