在 Node 專案使用 ESLint 分析工具並套用 Airbnb 程式碼風格

做法

建立專案。

1
2
mkdir my-project
cd my-project

初始化專案。

1
npm init

安裝依賴套件。

1
npm i eslint eslint-config-airbnb -D

建立 ESLint 設定檔。

1
npm init @eslint/config

修改 .eslintrc.js 檔。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
module.exports = {
env: {
commonjs: true,
es2021: true,
node: true,
},
extends: 'airbnb',
overrides: [
],
parserOptions: {
ecmaVersion: 'latest',
},
rules: {
},
};

新增 src/index.js 檔。

1
console.log('Hello World!');

修改 package.json 檔,新增 lint 指令。

1
2
3
4
5
{
"scripts": {
"lint": "eslint src"
}
}

執行檢查。

1
npm run lint