![](/img/trans.png)
[英]Parsing Error The Keyword import is Reserved (SublimeLinter-contrib-eslint)
[英]eslint: error Parsing error: The keyword 'const' is reserved
我從 ESLint 收到此錯誤:
error Parsing error: The keyword 'const' is reserved
從這段代碼:
const express = require('express');
const app = express();
const _ = require('underscore');
我試過刪除node_modules
並重新安裝所有 npm 包( 如此處所建議),但無濟於事。
ESLint 默認使用 ES5 語法檢查。
您將希望覆蓋到最新的受良好支持的 JavaScript 版本。
嘗試將.eslintrc.json
文件添加到您的項目中。 在里面:
{
"parserOptions": {
"ecmaVersion": "latest"
},
"env": {
"es6": true
}
}
希望這會有所幫助。
編輯:我還發現這個例子.eslintrc.json
可能會有所幫助。
您也可以添加這個內聯而不是配置,只需在添加自己的禁用內容之前將其添加到同一個文件中
/* eslint-env es6 */
/* eslint-disable no-console */
我的情況是禁用文件,而 eslint-disable 並不能單獨為我工作
/* eslint-env es6 */
/* eslint-disable */
我使用了 .eslintrc.js 並添加了以下代碼。
module.exports = {
"parserOptions": {
"ecmaVersion": 6
}
};
v7.30.0
使用 ESLint v7.30.0
,您可以使用latest
而不是2017
,它將啟用支持的最新 ECMAScript 版本。
"ecmaVersion": "latest" 總是在 ESLint 的默認解析器中啟用最新支持的 ECMAScript 版本。
.eslintrc.json
"parserOptions": {
"ecmaVersion": "latest"
}
如果使用 Visual Code,一個選項是將其添加到 settings.json 文件中:
"eslint.options": {
"useEslintrc": false,
"parserOptions": {
"ecmaVersion": 2017
},
"env": {
"es6": true
}
}
我的這部分代碼也有同樣的問題:
const newComment = {
dishId: dishId,
rating: rating,
author: author,
comment: comment
};
newComment.date = new Date().toISOString();
同樣的錯誤, const 是保留字。
問題是,我從您在更新中提供的鏈接制作了 .eslintrc.js,但仍然出現相同的錯誤。 此外,我在.eslintrc.js
中遇到解析錯誤: Unexpected token ':'
。
就在這一部分:
"env": {
"browser": true,
"node": true,
"es6": true
},
...
請在此文件.eslintrc.js 中使用此語法。 如果它不存在,那么你必須創建一個。 另外,請先通過編寫這些步驟npm install --save-dev eslint然后在項目根目錄中創建.eslintrc.js文件來查找有關如何將 eslint 作為開發依賴項安裝到當前項目的步驟。
{
"parserOptions": {
"ecmaVersion": 2017
},
"env": {
"es6": true
}
}
如已接受的答案中所述,我使用了.eslintrc.json中的配置:
{
parserOptions: {
"ecmaVersion": "latest"
},
env: {
"es6": true
}
}
但這還不夠,您還需要在.eslintrc.json文件中將覆蓋重命名為 overrideConfig :
"overrideConfig": [ <-- after renaming
{
"files": [
"*.ts"
],
"parserOptions": {
"project": [
"tsconfig.json",
"e2e/tsconfig.json"
],
"ecmaVersion": "latest",
"sourceType": "module",
"createDefaultProgram": true
},
"env": {
"es6": true
}
...
當然,前提是你的settings.json中省略了useEslintrc參數(或者設置為true)
我在更新時遇到了這個問題。 我在項目中也有一個 eslintrc.json。 我剛剛在 Visual Studio Code 中關閉了我的項目並重新打開它,錯誤就消失了。 似乎 VS Code 緩存。
就我而言,它無法找到.eslintrc
文件,因此我從 node_modules/.bin 復制到根目錄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.