ESlint - import.meta 导致致命解析错误
ESlint - import.meta causes Fatal Parsing Error
使用Atom editor, with the linter-eslint package installed, I have a node.mjs script that uses ES6 module's import语句导入各种节点模块。
当我 运行 使用节点的 --experimental-modules 标志时,脚本 运行 没问题。然而,在使用 Atom 编辑时,linter-eslint 说:
Parsing error: Unexpected token import (Fatal)
此解析错误 NOT 是由代码文件顶部的 ecmascript "import" 语句引起的。相反,它实际上是由于 eslint 将 "import" 视为只能在 import statements and therefore cannot be used by the import.meta 对象中使用的保留标记(如下面的代码行所示):
const __dirname = path.dirname(new URL(import.meta.url).pathname);
我的 .eslintrc.js 文件有这些解析器选项:
'parserOptions':
{
'ecmaVersion': 2018,
'sourceType': 'module'
}
如何配置 eslint 以忽略此特定错误?
我确定这不是最好的解决方案,但我这样做了...
首先创建另一个文件(我使用 Util.mjs)并将逻辑移动到那个...
import path from 'path';
const __dirname = path.dirname(new URL(import.meta.url).pathname);
export { __dirname }
然后创建.eslintignore
并添加{path}/Util.mjs
。最后在原来的 class...
中使用新导入
import {__dirname} from './Util.mjs';
我也刚遇到这个问题。在 eslint 7.2.0(2020 年 6 月)中添加了对 import.meta
的支持,但是为了使其正常工作,我必须编辑 .eslintrc.json
并将 ecmaVersion
从 2018
更改为 2020
.
@Malvineous 的回答对我有用,但我不确定将 emcaVersion
值放在 .eslintrc
文件中的什么位置。
它应该位于 .eslintrc
文件中 JSON 对象的顶层。
{
"rules": { ... },
"parserOptions": { "ecmaVersion": 2020 },
"settings": { ... }
}
使用Atom editor, with the linter-eslint package installed, I have a node.mjs script that uses ES6 module's import语句导入各种节点模块。
当我 运行 使用节点的 --experimental-modules 标志时,脚本 运行 没问题。然而,在使用 Atom 编辑时,linter-eslint 说:
Parsing error: Unexpected token import (Fatal)
此解析错误 NOT 是由代码文件顶部的 ecmascript "import" 语句引起的。相反,它实际上是由于 eslint 将 "import" 视为只能在 import statements and therefore cannot be used by the import.meta 对象中使用的保留标记(如下面的代码行所示):
const __dirname = path.dirname(new URL(import.meta.url).pathname);
我的 .eslintrc.js 文件有这些解析器选项:
'parserOptions':
{
'ecmaVersion': 2018,
'sourceType': 'module'
}
如何配置 eslint 以忽略此特定错误?
我确定这不是最好的解决方案,但我这样做了...
首先创建另一个文件(我使用 Util.mjs)并将逻辑移动到那个...
import path from 'path';
const __dirname = path.dirname(new URL(import.meta.url).pathname);
export { __dirname }
然后创建.eslintignore
并添加{path}/Util.mjs
。最后在原来的 class...
import {__dirname} from './Util.mjs';
我也刚遇到这个问题。在 eslint 7.2.0(2020 年 6 月)中添加了对 import.meta
的支持,但是为了使其正常工作,我必须编辑 .eslintrc.json
并将 ecmaVersion
从 2018
更改为 2020
.
@Malvineous 的回答对我有用,但我不确定将 emcaVersion
值放在 .eslintrc
文件中的什么位置。
它应该位于 .eslintrc
文件中 JSON 对象的顶层。
{
"rules": { ... },
"parserOptions": { "ecmaVersion": 2020 },
"settings": { ... }
}