[英]Eslint: How to disable "unexpected console statement" in Node.js?
我将 eslint 与 Sublime Text 3 一起使用,并且正在编写gulpfile.js
。
/*eslint-env node*/
var gulp = require('gulp');
gulp.task('default', function(){
console.log('default task');
});
但是 eslint 一直显示错误:“错误:意外的控制台语句。(无控制台)”
我在这里找到了官方文档,但我仍然不知道如何禁用它。
/*eslint-env node*/
var gulp = require('gulp');
/*eslint no-console: 2*/
gulp.task('default', function(){
console.log('default task');
});
也不行。
我的 Sublime Text 3 插件:SublimeLinter 和 SublimeLinter-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 */
对于vue-cli 3,打开package.json
并在eslintConfig
部分eslintConfig
将no-console
放在rules
下并重新启动开发服务器( npm run serve
或yarn serve
)
...
"eslintConfig": {
...
"rules": {
"no-console": "off"
},
...
更好的选择是根据节点环境使 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,
},
如果您只想禁用一行的规则,则以下适用于 VSCode 中的 ESLint。
要禁用下一行:
// eslint-disable-next-line no-console
console.log('hello world');
要禁用当前行:
console.log('hello world'); // eslint-disable-line no-console
如果你在本地项目下安装 eslint,你应该有一个目录 /node_modules/eslint/conf/ 并且在该目录下有一个文件 eslint.json。 您可以编辑文件并修改值为“off”的“no-console”条目(尽管也支持 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",
....
我希望这个“配置”可以帮助你。
如果您只想禁用一次规则,则需要查看Exception's answer 。
您可以通过仅禁用一行的规则来改进这一点:
...在当前行:
console.log(someThing); /* eslint-disable-line no-console */
...或在下一行:
/* eslint-disable-next-line no-console */
console.log(someThing);
我正在使用 Ember.js,它生成一个名为.eslintrc.js
的文件。 将"no-console": 0
到 rules 对象为我完成了这项工作。 更新后的文件如下所示:
module.exports = {
root: true,
parserOptions: {
ecmaVersion: 6,
sourceType: 'module'
},
extends: 'eslint:recommended',
env: {
browser: true
},
rules: {
"no-console": 0
}
};
在我的vue
项目中,我像这样解决了这个问题:
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"
]
}
如果根据文档配置 package.json 后仍然遇到问题(如果您选择使用 package.json 来跟踪而不是单独的配置文件):
"rules": {
"no-console": "off"
},
它仍然不适合您,不要忘记您需要返回命令行并再次执行 npm install 。 :)
或者,您可以允许,而不是关闭“无控制台”。 在.eslintrc.js文件中放入
rules: {
"no-console": [
"warn",
{ "allow": ["clear", "info", "error", "dir", "trace", "log"] }
]
}
这将允许您执行console.log
和console.clear
等操作而不会抛出错误。
在package.json 中,您会找到eslintConfig
行。 您的“规则”行可以像这样进入:
"eslintConfig": {
...
"extends": [
"eslint:recommended"
],
"rules": {
"no-console": "off"
},
...
},
2018 年 10 月,
做就是了:
// tslint:disable-next-line:no-console
别人回答
// eslint-disable-next-line no-console
不工作!
您应该添加一个规则并添加您的环境:
{
"rules": {
"no-console": "off"
},
"env": {
"browser": true
}
}
您可以添加其他环境。
将其放在项目位置的.eslintrc.js文件中对我有用
module.exports = {
rules: {
'no-console': 0
}
};
在“规则”,“无控制台”中:[假,“日志”,“错误”]
我的 2 美分贡献:
除了删除控制台警告(如上所示)之外,最好从 PROD 环境中删除您的日志(出于安全原因)。 我发现这样做的最好方法是将它添加到nuxt.config.js
build: {
terser: {
terserOptions: {
compress: {
//this removes console.log from production environment
drop_console: true
}
}
}
}
工作原理:Nuxt 已经使用 terser 作为缩小器。 此配置将强制 terser 在压缩期间忽略/删除所有控制台日志命令。
确保颤振项目所在文件夹的名称。没有空格。 那是我的错误
使用窗口对象
window.console.log("..")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.