我正在使用eslint与Sublime Text 3和我正在编写gulpfile.js。

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

gulp.task('default', function(){
    console.log('default task');
});

但是eslint一直显示error: " error: Unexpected console statement. "(no-console)”

我在这里找到了官方文件,但我仍然不知道如何禁用它。

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

/*eslint no-console: 2*/
gulp.task('default', function(){
    console.log('default task');
});

也不管用。

我的崇高文本3插件:升华和升华-contrib-eslint。

这是我的.eslintrc.js文件:

module.exports = {
    "rules": {
        "no-console":0,
        "indent": [
            2,
            "tab"
        ],
        "quotes": [
            2,
            "single"
        ],
        "linebreak-style": [
            2,
            "unix"
        ],
        "semi": [
            2,
            "always"
        ]
    },
    "env": {
        "browser": true,
        "node": true
    },
    "extends": "eslint:recommended"
};

在你的文件目录中创建一个.eslintrc.js,并将以下内容放入其中:

module.exports = {
    rules: {
        'no-console': 'off',
    },
};

你应该更新eslint配置文件来永久修复这个问题。否则,你可以临时启用或禁用eslint检查控制台如下所示

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

如果你在你的本地项目下安装eslint,你应该有一个目录/node_modules/eslint/conf/,在这个目录下有一个文件eslint.json。你可以编辑文件,并修改“no-console”条目的值为“off”(尽管也支持0值):

"rules": {
    "no-alert": "off",
    "no-array-constructor": "off",
    "no-bitwise": "off",
    "no-caller": "off",
    "no-case-declarations": "error",
    "no-catch-shadow": "off",
    "no-class-assign": "error",
    "no-cond-assign": "error",
    "no-confusing-arrow": "off",
    "no-console": "off",
    ....

我希望这个“配置”可以帮助你。

我使用Ember.js生成一个名为.eslintrc.js的文件。在rules对象中添加“no-console”:0对我来说就是这样。更新后的文件是这样的:

module.exports = {
  root: true,
  parserOptions: {
    ecmaVersion: 6,
    sourceType: 'module'
  },
  extends: 'eslint:recommended',
  env: {
    browser: true
  },
  rules: {
    "no-console": 0
  }
};

更好的选择是根据节点环境有条件地显示console.log和调试器语句。

  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,
  },

如果您只想禁用该规则一次,则需要查看Exception的答案。

你可以通过只对一行禁用规则来改善这一点:

... 在当前行中:

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

... 或者在下一行:

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

你应该添加一条规则并添加你的env:

{
  "rules": {
    "no-console": "off"
  },
  "env": {
    "browser": true
  }
}

您可以添加其他环境。

如果你想只对一行禁用规则,下面的方法可以在VSCode中使用ESLint。

禁用下一行:

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

禁用当前行。

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

在包中。你会发现一个eslintConfig行。你的“规则”行可以这样写:

  "eslintConfig": {
   ...
    "extends": [
      "eslint:recommended"
    ],
    "rules": {
      "no-console": "off"
    },
   ...
  },

vue-cli 3打开包。在eslintConfig章节中将no-console置于规则下并重新启动dev服务器(npm运行服务或yarn服务)

...
"eslintConfig": {
    ...
    "rules": {
      "no-console": "off"
    },
    ...

2018年10月,

只做:

// tslint:disable-next-line:no-console

其他人用

// eslint-disable-next-line no-console

不管用!

在我的vue项目中,我修复了这个问题:

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

Ps:包装。Json是vue项目目录下的一个configfile,最后的内容如下所示:

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

如果在配置了软件包后仍然有问题。Json,如果你选择使用package. Json。Json来跟踪而不是单独的配置文件):

"rules": {
      "no-console": "off"
    },

它仍然不能为你工作,不要忘记你需要回到命令行,再次执行npm install。:)

使用窗口对象

window.console.log(“。”)

或者你可以允许“无控制台”,而不是关闭“无控制台”。在.eslintrc.js文件中放入

  rules: {
    "no-console": [
     "warn",
     { "allow": ["clear", "info", "error", "dir", "trace", "log"] }
    ]
  }

这将允许您执行console.log和console.log。清除etc而不抛出错误。

在“规则”, "no-console": [false, "log", "error"]

我的2美分贡献:

除了删除控制台警告(如上所示),最好从PROD环境中删除您的日志(出于安全原因)。 我发现最好的方法是把它添加到nuext .config.js中

  build: {
   terser: {
      terserOptions: {
        compress: {
          //this removes console.log from production environment
          drop_console: true
        }
      }
    }
  }

工作原理:Nuxt已经使用terser作为缩略词。该配置将强制terser在压缩期间忽略/删除所有控制台日志命令。

请确保颤振项目所在文件夹的名称。没有空格。这是我的错误