Javascript eslint:错误解析错误:关键字'const'被保留

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42706584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:22:42  来源:igfitidea点击:

eslint: error Parsing error: The keyword 'const' is reserved

javascriptnode.jsecmascript-6eslint

提问by opike

I am getting this error from ESLint:

我从 ESLint 收到此错误:

error  Parsing error: The keyword 'const' is reserved

from this code:

从这个代码:

const express = require('express');
const app = express();
const _ = require('underscore');

I've tried removing node_modulesand reinstalling all npm packages (as suggested here), but to no avail.

我试过删除node_modules并重新安装所有 npm 包(如建议here),但无济于事。

回答by iamjpg

ESLint defaults to ES5 syntax-checking. You'll want to override to the latest well-supported version of JavaScript.

ESLint 默认使用 ES5 语法检查。您需要覆盖到最新的受良好支持的 JavaScript 版本。

Try adding a .eslintrcfile to your project. Inside it:

尝试将.eslintrc文件添加到您的项目。在里面:

{
    "parserOptions": {
        "ecmaVersion": 2017
    },

    "env": {
        "es6": true
    }
}

Hopefully this helps.

希望这会有所帮助。

EDIT: I also found this example .eslintrcwhich might help.

编辑:我还发现了这个.eslintrc可能有帮助的例子

回答by Khachornchit Songsaen

I used .eslintrc.js and I have added following code.

我使用了 .eslintrc.js 并添加了以下代码。

module.exports = {
    "parserOptions": {
        "ecmaVersion": 6
    }
};

回答by yousef

you also can add this inline instead of config, just add it to the same file before you add your own disable stuff

您也可以添加此内联而不是配置,只需在添加自己的禁用内容之前将其添加到同一个文件中

/* eslint-env es6 */
/* eslint-disable no-console */

my case was disable a file and eslint-disable were not working for me alone

我的情况是禁用文件并且 eslint-disable 不能单独为我工作

/* eslint-env es6 */
/* eslint-disable */

回答by Marcos Sevilla

I had this same problem with this part of my code:

我的这部分代码有同样的问题:

const newComment = {
    dishId: dishId,
    rating: rating,
    author: author,
    comment: comment
};
newComment.date = new Date().toISOString();

Same error, const is a reserved word.

同样的错误,const 是一个保留字。

The thing is, I made the .eslintrc.js from the link you gave in the update and still got the same error. Also, I get an parsing error in the .eslintrc.js: Unexpected token ':'.

问题是,我从您在更新中提供的链接制作了 .eslintrc.js,但仍然出现相同的错误。另外,我在.eslintrc.js: 中遇到解析错误Unexpected token ':'

Right in this part:

就在这部分:

"env": {
"browser": true,
"node": true,
"es6": true
},

...

回答by Bj?rnar Hvidsten

If using Visual Code one option is to add this to the settings.json file:

如果使用 Visual Code,一种选择是将其添加到 settings.json 文件中:

"eslint.options": {
    "useEslintrc": false,
    "parserOptions": {
        "ecmaVersion": 2017
    },
    "env": {
        "es6": true
    }
}

回答by fuddin

In my case, it was unable to find the .eslintrcfile so I copied from node_modules/.bin to root.

就我而言,它无法找到该.eslintrc文件,所以我从 node_modules/.bin 复制到 root。