PricingSign inDownload

Blog / claude code

Make Claude Code ask before it adds a dependency

Ask rules for npm, pnpm, yarn and pip, plus one for package.json itself, so a new package never lands without a yes. Tested both ways.

On this page
  1. What happens without a rule?
  2. The rules
  3. What happens with the rules?
  4. Other ecosystems
  5. Why a new package deserves a yes
  6. Tell the agent as well as stopping it
  7. Shared or personal?
  8. Check the result anyway
  9. When the yes belongs to someone else
  10. Frequently asked questions
  11. How do I stop Claude Code from installing npm packages?
  12. Does an allow rule override an ask rule?
  13. Can Claude Code still add a dependency by editing package.json?
  14. Should I use deny instead of ask?
  15. Try it with your team

Coding agents reach for packages. Ask for padding and you may get a dependency; ask for a date picker and you get three. Each one is code you now ship, update and answer for, and some are deprecated, abandoned or worse. You want the agent to edit code on its own, but a new dependency should be your call.

Claude Code can do exactly that with "ask" rules in the project settings. We tested them with Claude Code 2.1.318 against a small npm project, with and without the rules.

What happens without a rule?#

We gave Claude Code permission to edit files and to run npm, and asked:

bash
claude -p "Make pad.js left-pad strings properly. Use the left-pad package from npm." \
  --permission-mode acceptEdits --allowedTools "Bash(npm:*)"
git status --short
 M package.json
 M pad.js
?? node_modules/
?? package-lock.json

It installed the package. Fair enough: we asked it to. The point is that nothing stood between the request and a new entry in package.json.

The rules#

Add this to .claude/settings.json in the project and commit it, so it applies to everyone who works in the repository:

json
{
  "permissions": {
    "ask": [
      "Bash(npm install:*)",
      "Bash(npm i:*)",
      "Bash(npm add:*)",
      "Bash(pnpm add:*)",
      "Bash(yarn add:*)",
      "Bash(pip install:*)",
      "Edit(package.json)"
    ]
  }
}

The Bash rules catch the install commands. The Edit(package.json) rule catches the other route: an agent that can't run npm install can still write the dependency into the file by hand.

What happens with the rules?#

The same command, with npm still allowed on the command line:

bash
claude -p "Make pad.js left-pad strings properly. Use the left-pad package from npm." \
  --permission-mode acceptEdits --allowedTools "Bash(npm:*)"
git status --short

Nothing changed. "Ask" beats "allow", so Bash(npm:*) didn't let the install through. Claude Code described the edit and the install it wanted and stopped. In an earlier run it also pointed out that left-pad is deprecated and that String.prototype.padStart does the same job without a package: the kind of answer you only get when the agent has to ask.

Then we tried the hand-written route:

bash
claude -p "Add left-pad ^1.3.0 to the dependencies in package.json by editing the file directly. Do not run npm." --permission-mode acceptEdits

It asked for write access to package.json and changed nothing. We added a web/package.json and asked again: blocked there too, so the rule covers package files in subfolders.

In a normal interactive session, "ask" means you get a prompt and decide. With -p, there is no one to ask, so the action is refused. That makes the same rules safe for scheduled and scripted runs.

Other ecosystems#

Add the install commands and manifest your project uses, the same way:

  • Python: Bash(pip install:*), Bash(uv add:*), Bash(poetry add:*), Edit(pyproject.toml), Edit(requirements.txt)
  • Rust: Bash(cargo add:*), Edit(Cargo.toml)
  • Go: Bash(go get:*), Edit(go.mod)

npm ci isn't on the list: it installs exactly what the lockfile already says, so agents can still set up the project.

Why a new package deserves a yes#

It isn't only about bloat. Installing a package runs code: npm packages can have install scripts that run on your machine the moment they land, before anyone has read a line. A name one letter off a popular package can be someone else's. And an agent picks packages the way a search does, by what it has seen most, which is often an old favourite that its own authors have since retired. left-pad in our test is one of those.

None of that means agents should never add dependencies. It means a person should see the name, the version and the reason before it happens, which is exactly what an ask rule gives you.

Tell the agent as well as stopping it#

A rule stops the action; a line in the project's instructions changes what the agent tries in the first place. Add one to CLAUDE.md or AGENTS.md:

markdown
Prefer the standard library and the packages already in package.json.
If a new dependency would really help, say which one and why, and wait for a yes.

With both in place, the agent usually proposes the package in its answer instead of hitting the rule mid-task, and you decide with the reason in front of you.

Shared or personal?#

Put the rules in .claude/settings.json and commit them when the whole team wants the same line. Use .claude/settings.local.json, which stays out of git, if it's only your preference. Rules from both files apply together, so a teammate can be stricter for themselves without changing the shared file.

Check the result anyway#

Rules cover the paths you thought of. Before merging, read the manifest diff:

bash
git diff main -- package.json package-lock.json

A lockfile change with no package.json change is worth a second look too; our post on an agent that rewrote package-lock.json covers why it happens.

When the yes belongs to someone else#

On a team, the person who should approve a new dependency is often the one who maintains the build, not the one running the agent. Joxo sends an agent's question to the right person, on their phone or desktop, and the answer goes back into the session, so "can we add this package?" doesn't wait for a meeting. See approving agent actions from your phone.

Frequently asked questions#

How do I stop Claude Code from installing npm packages?#

Add ask rules to .claude/settings.json: Bash(npm install:*), Bash(npm i:*), Bash(npm add:*) and Edit(package.json), plus pnpm add and yarn add if you use them. Interactive sessions then ask you first, and claude -p runs refuse.

Does an allow rule override an ask rule?#

No. In our test, an ask rule for npm install held even with Bash(npm:*) allowed on the command line. Ask rules win over allow rules.

Can Claude Code still add a dependency by editing package.json?#

Not with an Edit(package.json) ask rule. In our test it asked for write access instead of editing, both for the root package.json and for one in a subfolder.

Should I use deny instead of ask?#

Use ask if you want to say yes sometimes: you get a prompt and decide. Use deny if dependencies are never added through the agent. Either way, -p runs refuse.