Skip to content

hookDevOps and cloud

Block Global Installs

A Claude Code hook that refuses global npm, pnpm, yarn, pip and gem installs so tools are added to the project and not to your whole machine.

npx workspacetemplates add block-global-installs --tool claude-code

Writes 1 file to .claude/

.claude/settings.jsonmerge into your file16 lines · 1.1 KBFile
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "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 c=String(i.command||'').replace(/\\x22[^\\x22]*\\x22|\\x27[^\\x27]*\\x27/g,' ')+' ';const e=process.env.VIRTUAL_ENV;const rules=[[/(^|[ ])npm[ ]+(i|install|add)[ ].*(-g|--global)[ ]/.test(c),'npm install -g'],[/pnpm[ ]+(add|install|i)[ ].*(-g|--global)[ ]/.test(c),'pnpm add -g'],[/yarn[ ]+global[ ]/.test(c),'yarn global'],[/gem[ ]+install[ ]/.test(c)&&!/--user-install|--install-dir|bundle/.test(c),'gem install'],[/pip3?[ ]+install[ ]/.test(c)&&!e&&!/venv|--target|[ ]-t[ ]/.test(c),'pip install outside a virtual environment']];const h=rules.find(r=>r[0]);if(h){console.error('Blocked: '+h[1]+' would change the whole machine. Install the tool in the project instead (devDependencies, a virtualenv, or the Gemfile) and run it through npx, python -m or bundle exec.');process.exitCode=2}})\"",
            "timeout": 10
          }
        ]
      }
    ]
  }
}

What it does

An agent that needs a missing tool will sometimes install it globally, which changes your machine and hides the dependency from the next person. Each shell call is inspected first, and the machine-wide forms are turned away.

It catches npm and pnpm with a global flag, yarn global, gem install without a user or bundle setup, and pip install when no virtual environment is active. The message tells the agent to add the tool to the project instead.

Example

you › Install typescript globally so tsc works everywhere.

agent › The global install is refused. The agent adds typescript as a dev dependency and calls it with npx tsc instead.

Questions

Why does pip install get blocked at times?
The hook checks the VIRTUAL_ENV variable. Activate your virtualenv before starting Claude Code, or call the venv pip by path, and it passes.
Does a local npm install still work?
Yes. Only commands with a global flag are refused, so plain installs inside the project are untouched.
  • Block npm Publish

    Hooknode

    Free
  • Check Node Version

    Hooknode

    Free
  • Protect CI Workflows

    Hookgithub-actions

    Free
  • Protect Lockfiles

    HookDevOps and cloud

    Free