need a correct eslintrc for async/await - using 7.6+ nodejs

浪尽此生 提交于 2019-12-18 05:39:10

问题


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

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

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

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?)

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"
        ]
    }
};

What is the fix?

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).

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.


回答1:


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

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


来源:https://stackoverflow.com/questions/44170348/need-a-correct-eslintrc-for-async-await-using-7-6-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!