Skip to content

hookFrontendjavascript

Warn Console Log

A Claude Code hook that warns the agent when an edit adds console.log to a JavaScript, TypeScript, Vue or Svelte file, so debug output is not left behind.

npx workspacetemplates add warn-console-log --tool claude-code

Writes 1 file to .claude/

.claude/settings.jsonmerge into your file16 lines · 935 BFile
{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "node -e \"let d='';process.stdin.on('data',x=>d+=x).on('end',()=>{let j={};try{j=JSON.parse(d)}catch(e){return}const i=j.tool_input||{};const f=String(i.file_path||'');if(!/[.](js|jsx|ts|tsx|mjs|cjs|vue|svelte)[ ]/i.test(f+' ')||/[.](test|spec|stories)[.]/i.test(f))return;const t=String(i.new_string||i.content||'');if(!/console[.]log[ ]*[(]/.test(t))return;let n='';try{n=require('fs').readFileSync(f,'utf8').split(String.fromCharCode(10)).map((l,k)=>/^[ ]*console[.]log/.test(l)?k+1:0).filter(Boolean).slice(0,5).join(', ')}catch(e){}console.error('Warning: console.log was added to '+f+(n?' (lines '+n+')':'')+'. Remove it or switch to the project logger before finishing.');process.exitCode=2})\"",
            "timeout": 10
          }
        ]
      }
    ]
  }
}

What it does

Debug prints have a way of surviving into commits. This PostToolUse hook looks at the text each Edit or Write call adds, and when it contains console.log it sends the agent a message with the file and the line numbers.

The file has already been saved, so the message works as a nudge, not a block. Test, spec and stories files are skipped, since logging there is often deliberate.

Example

you › Find out why the cart total is wrong.

agent › The agent adds a console.log to cart.ts. The hook reports the line, and the agent removes the print once the cause is found.

Questions

Is the edit undone?
No. The change is already written. The hook only tells the agent, which then removes the line on its own.
Does it flag console.error or console.warn?
No. Only console.log is matched, because the other methods are often used on purpose for real error reporting.
  • Bundle Size Reduction

    Skilljavascript

    Free