【问题标题】:Why is linting failing with "Unexpected token ." on "import.meta.url"?为什么 linting 因“Unexpected token ”而失败。在“import.meta.url”上?
【发布时间】:2020-06-27 02:53:36
【问题描述】:

我有以下 lint 配置...

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

还有下面的代码……

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

但是当它掉毛时,我得到...

9:46  error  Parsing error: Unexpected token .

这是一段很常见的代码,所以我很困惑。

更新

解决了...

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

但我想要一个不必忽略整个文件的解决方案。

【问题讨论】:

  • 这能回答你的问题吗? ESlint - import.meta causes Fatal Parsing Error。你会想要使用babel-eslint
  • 这基本上就是我所做的,但我觉得这不是一个合法的答案。感谢您找到它!
  • 哦!我什至没有注意到原来的答案实际上来自你!我只是试了一下,设置 "parser": "babel-eslint""env": { "node": true } 对我有用。让我知道这是否也适合您,我会将其添加为答案。
  • 啊,你需要npm i -D babel-eslint
  • 谢谢,但在我的情况下,babel 不是一个选项

标签: node.js eslint


【解决方案1】:

这是一个语法错误,因为 ESLint 的默认解析器只支持第 4 阶段提案,但import.meta 目前是第 3 阶段。现在,您必须将解析器更改为“babel-eslint”或“@typescript-eslint/解析器”以解析import.meta

该短语是语法错误,因为import 是 EcmaScript 中的关键字。因此import.metaif.fooswitch.foo 一样无效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 2022-06-16
    • 2012-09-08
    • 2013-09-24
    • 2011-07-23
    相关资源
    最近更新 更多