ESlint - import.meta causes Fatal Parsing Error
Asked Answered
P

3

19

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.

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?

Petropavlovsk answered 24/1, 2019 at 0:11 Comment(5)
@LonnieBest Did you try the "parser": "babel-eslint" eslint config as in the linked issue?Pruitt
@Pruitt : I did; it lead to another issue.Petropavlovsk
@LonnieBest You would of course have to pass all 4 options to the parserOptions.Pruitt
@Pruitt : I think you've discovered my issue. I'm setting the parser with eslint.parserOptions.parser = 'babel-eslint'. However, I should be setting it how you said: eslint.parser = 'babel-eslint'. These properties are of the main eslint object; they're NOT properties of the parserOptions object.Petropavlovsk
ES Lint Bug 13133Petropavlovsk
B
23

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.

Babbage answered 2/1, 2021 at 16:49 Comment(0)
R
9

@Malvineous's answer worked for me, but I was unsure of where to put the emcaVersion value in the .eslintrc file.

It should go in the top level of the JSON object in the .eslintrc file.

{ 
    "rules": { ... }, 
    "parserOptions": { "ecmaVersion": 2020 }, 
    "settings": { ... }
} 
Rhododendron answered 5/4, 2021 at 0:22 Comment(0)
K
2

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';
Kennard answered 31/10, 2019 at 15:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.