ESlint - import.meta 导致致命解析错误 [英] ESlint - import.meta causes Fatal Parsing Error

查看:93
本文介绍了ESlint - import.meta 导致致命解析错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using the Atom editor, with the linter-eslint package installed, I have a node.mjs script that uses ES6 module's import statement to import various node modules.

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

The script runs fine when I run it with node's --experimental-modules flag. However, while editing with Atom, linter-eslint says:

Parsing error: Unexpected token import (Fatal)

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

This parsing error is NOT being caused by the ecmascript "import" statements that I have at the top of my code file. Instead, it is actually caused by the fact that eslint considers "import" a reserved token that can only be used in import statements and therefore cannot be used by the import.meta object (as shown in this code line below):

const __dirname = path.dirname(new URL(import.meta.url).pathname);

我的 .eslintrc.js 文件有以下解析器选项:

My .eslintrc.js file has these parser options:

'parserOptions':
{
    'ecmaVersion': 2018,
    'sourceType': 'module'
}

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

How can I configure eslint to ignore this particular error?

推荐答案

我也刚遇到这个问题.在 eslint 7.2.0 中添加了对 import.meta 的支持(2020 年 6 月)但是为了使它工作,我必须编辑 .eslintrc.json 并将 ecmaVersion2018 更改为2020.

I just came across this issue too. Support for import.meta was added in eslint 7.2.0 (June 2020) however in order to make it work I had to edit .eslintrc.json and change ecmaVersion from 2018 to 2020.

这篇关于ESlint - import.meta 导致致命解析错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆