ESLintを使ってプロジェクト内のconsole.logを見つける

デバッグの時に便利なconsole.log(console.errorなど)ですが、消し忘れてプロダクションに残ったままになることがあります。

ESLintのno-consoleというルールを設定することで、これを検知できます。

以下は警告を表示するための設定例です。

module.exports = {
    "env": {
        "browser": true,
        "commonjs": true,
        "es2021": true
    },
    "extends": "eslint:recommended",
    "parserOptions": {
        "ecmaVersion": 13
    },
    "rules": {
        "no-console": "warn" // これを追加
    }
};

console.logが残っていると、lint実行時に以下の警告が表示されます。

3:1  warning  Unexpected console statement  no-console