ESlint-import.meta导致致命解析错误 [英] ESlint - import.meta causes Fatal Parsing Error
问题描述
使用 Atom 编辑器和 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)
此解析错误是不是,是由于我在代码文件顶部的ecmascript"import"语句引起的.相反,它实际上是由eslint认为导入"一个保留令牌的事实引起的,该令牌只能在 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?
推荐答案
我确定这不是最好的解决方案,但我做到了...
I am sure the is not the best solution but I did this...
首先创建另一个文件(我使用了Util.mjs),然后将逻辑移到该文件中.
First create another file (I used Util.mjs) and move the logic into that...
import path from 'path';
const __dirname = path.dirname(new URL(import.meta.url).pathname);
export { __dirname }
然后创建.eslintignore
并添加{path}/Util.mjs
.最后在您的原始类中使用新的导入...
Then create .eslintignore
and add {path}/Util.mjs
. Finally use the new import in your original class...
import {__dirname} from './Util.mjs';
这篇关于ESlint-import.meta导致致命解析错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!