ESlint - import.meta causes Fatal Parsing Error

非 Y 不嫁゛ 提交于 2019-12-07 23:17:12

问题


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

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)

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);

My .eslintrc.js file has these parser options:

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

How can I configure eslint to ignore this particular error?


回答1:


I am sure the is not the best solution but I did this...

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 }

Then create .eslintignore and add {path}/Util.mjs. Finally use the new import in your original class...

import {__dirname} from './Util.mjs';


来源:https://stackoverflow.com/questions/54337576/eslint-import-meta-causes-fatal-parsing-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!