Why is linting failing with "Unexpected token ." on "import.meta.url"?
Asked Answered
C

2

6

I have the following lint config...

{
  "extends": ["eslint:recommended", "google"],
  "parserOptions": {
    "ecmaVersion": 2020,
    "sourceType": "module"
  },
  "rules": {
    "require-jsdoc": 1
  },
  "env": {
    "es6": true
  }
}

and the following code...

const __dirname = path.dirname(new URL(import.meta.url).pathname);
                                           //^Error is...

But when it lints I get...

9:46  error  Parsing error: Unexpected token .

This is a pretty common piece of code so I am confused.

Update

solved with...

"ignorePatterns": ["unclean.mjs", "node_modules"],

But I would like a solution where I don't have to ignore an entire file.

Crepe answered 15/3, 2020 at 22:46 Comment(5)
Does this answer your question? ESlint - import.meta causes Fatal Parsing Error. You'll want to use babel-eslint.Neuro
That is basically what I did but I don't feel like it is a legitimate answer. Thank you for finding that!Crepe
Oh! I didn't even notice that the original answer was actually from you! I just gave it a try and setting "parser": "babel-eslint" and "env": { "node": true } worked for me. Let me know if that works for you too, I'll add it as an answer.Neuro
Ah, and you'll need to npm i -D babel-eslintNeuro
Thanks but babel is not an option in my caseCrepe
I
4

It is a syntax error because the default parser of ESLint only supports stage 4 proposals, but import.meta is currently stage 3. For now, you must change the parser to "babel-eslint" or "@typescript-eslint/parser" in order to parse import.meta.

That phrase is a syntax error because import is a keyword in EcmaScript. Therefore import.meta is as invalid as if.foo or switch.foo.

Integrand answered 19/3, 2020 at 0:57 Comment(0)
N
0

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.

Notarial answered 21/10 at 16:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.