ESlint - import.meta 导致致命解析错误

Lon*_*est 8 javascript node.js eslint atom-editor es6-modules

使用Atom编辑器,安装了 linter -eslint包后,我有一个 node.mjs 脚本,它使用 ES6 模块的import语句导入各种节点模块。

当我使用节点的--experimental-modules标志运行脚本时,脚本运行良好。但是,在使用 Atom 进行编辑时,linter-eslint 说:

Parsing error: Unexpected token import (Fatal)
Run Code Online (Sandbox Code Playgroud)

这个解析错误不是由我的代码文件顶部的 ecmascript“import”语句引起的。相反,它实际上是由于 eslint 认为“import”是一个保留令牌,只能在import 语句中使用,因此不能被import.meta对象使用(如下面的代码行所示):

const __dirname = path.dirname(new URL(import.meta.url).pathname);
Run Code Online (Sandbox Code Playgroud)

我的 .eslintrc.js 文件有这些解析器选项:

'parserOptions':
{
    'ecmaVersion': 2018,
    'sourceType': 'module'
}
Run Code Online (Sandbox Code Playgroud)

如何配置 eslint 以忽略此特定错误?

小智 9

@Malvineous 的答案对我有用,但我不确定将该emcaVersion值放在文件中的位置.eslintrc

它应该位于文件中 JSON 对象的顶层.eslintrc

{ 
    "rules": { ... }, 
    "parserOptions": { "ecmaVersion": 2020 }, 
    "settings": { ... }
} 
Run Code Online (Sandbox Code Playgroud)


Mal*_*ous 7

我也刚遇到这个问题。支持import.meta在加入eslint 7.2.0(六月2020年)然而,为了使它工作,我不得不修改.eslintrc.json和变化ecmaVersion,从20182020