Eslint with Sails
Created at 2018-04-26 Updated at 2024-11-07 - 2 min. read
Linting code is already an established part of any (popular) JavaScript project and, in my opinion, has a lot of benefits such as:
- Readability
- Pre-code review
- Finding (syntax) errors before execution
A common set of rules makes it easier to really understand what the code is doing.
Further linting rules help to improve code reviews, as linting already acts as a pre-code review, checking against all the basic issues such as syntax errors, incorrect naming, the tab vs. spaces debate, etc. It increases the value of having code reviews, as people are then more willing to check the implementation rather than complain about syntax errors.
When the task was assigned to me to run eslint with sails. I searched the web for many options. It ended with the following approach.
- Install sails-generate-eslnitrc
- Run
node_modules/.bin/sails-generate-eslintrc
. This will generateeslintrc-sails
file in the root of your project. Remember to run the code from the root of your project. In some cases, it might throw an error asCan't found the eslintrc file
. In this case, create.eslintrc
file in the root and run the command again. - Now install sails-eslint and eslint-config-google. We would be using google linting rules with some our custom rules.
Replace the
.eslintrc.json
with the following format1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28{
"env": {
"node": true,
"es6": true
},
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module",
"ecmaFeatures": {
"jsx": false
}
},
"globals": {
"_": true,
"sails": true,
"async": true,
"server": true
},
"extends":[
"eslint:recommended",
"google"
],
"rules":{
"no-unused-vars": "warn",
"no-console": 0,
"camelcase":"off"
}
}Replace globals in
.eslintrc.json
from.eslintrc-sails
.- Now run your app by
node app.js
and fix the lint issue.
One can solve the major issue by running node_modules/.bin/eslint * --fix
. You can also run for a specific folder by node_modules/.bin/eslint api/* --fix