1. Global installation
npm install -g eslint
2. Add configuration file
eslint:recommended
Rules are general rules and the most basic coding standards.
.eslintrc.js
module.exports = {
extends: 'eslint:recommended',
};
3. Create a new test file
Create a new test.js file, add a little js code
function jddk(){
return 6666;
}
console.log(jddk())
4. Try rule verification
Run in cmd
eslint test.js
The console is undefined. This is because JavaScript has a variety of runtime environments, and the console object may not exist in these runtime environments (the object of the window is also)
5. Specify the operating environment
.eslintrc.js
module.exports = {
extends: 'eslint:recommended',
env:{
//浏览器环境
browser:true,
//nodejs环境
node:true,
es6:true
}
};
Run it after adding rules
6. Add some custom rules
Set tab indentation (set the default tab indentation for tab indentation to 4)
.eslintrc.js
module.exports = {
extends: 'eslint:recommended',
env:{
//浏览器环境
browser:true,
//nodejs环境
node:true,
es6:true
},
rules:{
"index":["error","tab"]
}
};
In order to verify whether it takes effect, modify the test.js code format
function jddk(){
return 6666;
}
console.log(jddk())
Validation results