Safeguard
Developer Tools•

Is npx safe? What it downloads and runs, and how to use it carefully

npx runs a local package if one exists, otherwise downloads and runs registry code after a prompt. See what the yes flag skips and how to cut the risk.

Safeguard Security Team
3 min read

npx runs a package binary. If the package is not in your project, npx downloads it from the registry into the npm cache and runs it, which means running someone else's code with your permissions. It asks first by default. Use the yes flag only for names you have already verified.

How npx decides what to run

The npm documentation says npx checks local dependencies first. A bare package name matches whatever version is installed locally. If a version is specified, it only matches a local dependency with that exact name and version.

If the package is not local, npx installs it into a folder in the npm cache and adds that folder to the PATH of the command it runs. That is a real install, including dependencies, and it can trigger lifecycle scripts like any other.

The prompt and the yes flag

Before installing, npx prints a prompt. The docs describe it as a safeguard against mistyped package names. The yes flag skips the prompt and proceeds. A no flag skips the prompt and does not install.

This matters because typosquatting depends on a rushed command. A single wrong letter in a copied snippet, run with the yes flag in a script, executes whatever sits under that name. See what typosquatting is in package registries.

Safer habits

  1. Prefer installing tools as devDependencies and running the local copy. The lockfile then pins them.
  2. Before running an unfamiliar package, look at its publisher and history using npm view, as described in is this npm package safe to install.
  3. Pin a version in the command instead of taking the latest.
  4. Keep the yes flag out of shared docs and CI unless the package name and version are fixed and reviewed.
  5. Run one-off tools in a throwaway container or a machine with no production credentials.
SituationRisk levelSuggested approach
Local devDependencyLowerRun through npx or a package script
Pinned name and version, reviewedMediumAcceptable, with scripts reviewed
Unpinned name pasted from a chat or blogHigherVerify first, run in isolation

Automation is where this gets worse. Agents and tools that launch servers through npx commands from configuration files run the same download-and-execute step with no human watching, so review those configs like code.

Common mistakes

Teams often copy an npx command from a README into a CI file and add the yes flag so it does not hang. That removes the one prompt designed to catch a mistyped name. Another habit is running the bare name with no version, which takes whatever the registry offers on the day. A third is running npx on a laptop that holds cloud keys and publish tokens, where one bad package can reach all of them. A short allowlist of approved tools, with versions, solves most of this.

Quick check

Does npx always download from the registry?

No. It uses a matching local dependency first. It downloads to the npm cache only when the package is not found locally.

What does the npx yes flag do?

It skips the confirmation prompt and proceeds with the installation.

Safeguard's MCP Guardrail and policies help control which tools agents can launch, and the free scan at gold.safeguard.sh/scan checks the dependencies in a project.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.