Eslint: How to disable “unexpected console statement” in Node.js?

后端 未结 17 1580
遇见更好的自我
遇见更好的自我 2020-12-07 07:39

I\'m using eslint with Sublime Text 3 and I am writing gulpfile.js.

/*eslint-env node*/
var gulp = require(\'gulp\');

gulp.task(\'default\', fu         


        
相关标签:
17条回答
  • 2020-12-07 08:04

    Create a .eslintrc.js in the directory of your file, and put the following contents in it:

    module.exports = {
        rules: {
            'no-console': 'off',
        },
    };
    
    0 讨论(0)
  • 2020-12-07 08:05

    A nicer option is to make the display of console.log and debugger statements conditional based on the node environment.

      rules: {
        // allow console and debugger in development
        'no-console': process.env.NODE_ENV === 'production' ? 2 : 0,
        'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0,
      },
    
    0 讨论(0)
  • 2020-12-07 08:06

    The following works with ESLint in VSCode if you want to disable the rule for just one line.

    To disable the next line:

    // eslint-disable-next-line no-console
    console.log('hello world');
    

    To disable the current line:

    console.log('hello world'); // eslint-disable-line no-console
    
    0 讨论(0)
  • 2020-12-07 08:10

    in my vue project i fixed this problem like this :

    vim package.json
    ...
    "rules": {
        "no-console": "off"
    },
    ...
    

    ps : package.json is a configfile in the vue project dir, finally the content shown like this:

    {
      "name": "metadata-front",
      "version": "0.1.0",
      "private": true,
      "scripts": {
        "serve": "vue-cli-service serve",
        "build": "vue-cli-service build",
        "lint": "vue-cli-service lint"
      },
      "dependencies": {
        "axios": "^0.18.0",
        "vue": "^2.5.17",
        "vue-router": "^3.0.2"
      },
      "devDependencies": {
        "@vue/cli-plugin-babel": "^3.0.4",
        "@vue/cli-plugin-eslint": "^3.0.4",
        "@vue/cli-service": "^3.0.4",
        "babel-eslint": "^10.0.1",
        "eslint": "^5.8.0",
        "eslint-plugin-vue": "^5.0.0-0",
        "vue-template-compiler": "^2.5.17"
      },
      "eslintConfig": {
        "root": true,
        "env": {
          "node": true
        },
        "extends": [
          "plugin:vue/essential",
          "eslint:recommended"
        ],
        "rules": {
            "no-console": "off"
        },
        "parserOptions": {
          "parser": "babel-eslint"
        }
      },
      "postcss": {
        "plugins": {
          "autoprefixer": {}
        }
      },
      "browserslist": [
        "> 1%",
        "last 2 versions",
        "not ie <= 8"
      ]
    }
    
    0 讨论(0)
  • 2020-12-07 08:12

    If you just want to disable the rule once, you want to look at Exception's answer.

    You can improve this by only disabling the rule for one line only:

    ... on the current line:

    console.log(someThing); /* eslint-disable-line no-console */
    

    ... or on the next line:

    /* eslint-disable-next-line no-console */
    console.log(someThing);
    
    0 讨论(0)
  • 2020-12-07 08:15

    For vue-cli 3 open package.json and under section eslintConfig put no-console under rules and restart dev server (npm run serve or yarn serve)

    ...
    "eslintConfig": {
        ...
        "rules": {
          "no-console": "off"
        },
        ...
    
    0 讨论(0)
提交回复
热议问题