需要正确的eslintrc进行异步/等待-使用7.6+ nodejs [英] need a correct eslintrc for async/await - using 7.6+ nodejs

查看:164
本文介绍了需要正确的eslintrc进行异步/等待-使用7.6+ nodejs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用最新版本的nodejs 7.6+,我开始使用async/await.

With the latest version of nodejs 7.6+ I started using async/await.

我使用的是jshint,但据我所知,它们目前确实支持这种语法,有些建议使用eslint.

I was using jshint but from what I read they currently do support this syntax and some suggested using eslint.

好吧,我设置了eslint,但是argh ..它也标记了异步功能.
Parsing error: Unexpected token init (Fatal)

So ok I set eslint up but argh.. it flags async functions too.
Parsing error: Unexpected token init (Fatal)

我知道这没什么错,因为我的代码运行良好,只是个lint.如果我注释掉一个异步函数,它也只会标记下一个函数.实际上eslint仅标记发现此错误的第一个异步,而不是全部标记(这是怎么回事?)

I know there is nothing wrong as my code is running fine it's just the linter. Too if I comment out an async function it just flags the next one. IN fact eslint only flags the first async found with this error not all of them (what up with that?)

这是使用初始化向导制作的eslintrc文件.我希望只要求提供node和es6的env就足够了……显然没有.

Here is the eslintrc file made using the init wizard. I was hoping just asking for node and es6 for env would be sufficient...apparently not.

module.exports = {
    "env": {
        "es6": true,
        "node": true
    },
    "extends": "eslint:recommended",
    "rules": {
        "indent": [
            "error",
            "tab"
        ],
        "linebreak-style": [
            "error",
            "unix"
        ],
        "quotes": [
            "error",
            "single"
        ],
        "semi": [
            "error",
            "never"
        ]
    }
};

解决方法是什么?

我尝试了.eslintrc的多个版本,甚至看到eslint回购中存在一些相关问题,但是没有一个问题可以帮助我解决此问题.我不认为只是缺少一些关于使用commonjs(无babel)为本机nodejs正确设置eslint的错误.

I've tried several versions of .eslintrc and even saw there are a few issues related at the eslint repo but none are helping me to resolve this. I don't think it's a bug just missing something about getting eslint set up correctly for native nodejs using commonjs (no babel).

即使我不使用babel,谁知道也许需要babel插件才能完成此工作???如果是这样,我该如何设置.

Who knows maybe babel plugin is required to make this work even though I am not using babel??? If that's true how do I set that up.

推荐答案

由于async/await是ES2017功能,因此需要将其添加到.eslintrc.js:

Since async/await is an ES2017 feature, you need to add that to your .eslintrc.js:

module.exports = {
    // ...
    "parserOptions": {
        "ecmaVersion": 2017
    },
    // ...
}

这篇关于需要正确的eslintrc进行异步/等待-使用7.6+ nodejs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆