Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

node.js

eslint

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.

like image 847
Jackie Avatar asked Nov 06 '22 09:11

Jackie


1 Answers

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.

like image 78
golopot Avatar answered Nov 14 '22 22:11

golopot