# npx (Package Runner)

> How to execute CLI tools without polluting your system with global installs.

Subject: Package Managers | Lesson 11 of 24 | 3 min read
Interactive lesson: https://codewithanimation.in/package-managers/npx-and-package-exec
Previous: [Global vs Local Installs](https://codewithanimation.in/package-managers/global-vs-local-install.md) | Next: [npm Scripts & Lifecycle Hooks](https://codewithanimation.in/package-managers/npm-scripts-and-lifecycle.md)

## What is npx?

Modern npx is the npm package runner built on npm exec. It provides a way to invoke package executables without requiring a separate global installation. Executed package code still needs to be trusted.

A local CLI can also be invoked through an npm script. npx provides a convenient command-line path to an available local package or an explicitly requested package version.

## The Power of Temporary Execution

The true power of `npx` is how it handles packages that are **not** installed on your machine.

For a package not available locally, npm exec can fetch it into an npm cache directory and add its executables to the command's PATH. Cached files may remain for reuse; execution does not imply automatic cache deletion.

Choose a package name and version deliberately. An unversioned request can reuse a matching local package; @latest requests a registry tag, while an explicit version requests that version.

## The Execution Priority

Current npx uses npm exec package selection rather than the historical global-PATH-first algorithm.

1. Matching local packages can supply their installed executables. A specified version must match the local package to be reused as that request.

2. Missing requested packages can be placed in the npm cache. Interactive prompting and flags such as --yes or --no control consent to that fetch, not a security review.

3. npm runs the selected command with the relevant local/cache executable paths available. The command may still inherit other PATH entries; this is not a package sandbox.

## Common mistake: The typo trap

Check package spelling and origin before execution. A mistyped name can select an unintended package; accepting a fetch prompt or using --yes does not establish that its code is safe.

## Try it: npx Execution Simulator

Follow a local execution simulation that checks an available tool or fetches a requested package into cache. Its final cleanup step represents the command finishing, not a promise that npm deletes cached files.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/package-managers/npx-and-package-exec

## Check yourself

### Question 1

A matching CLI package is already installed locally. What can current npx do?

- A. Always download and replace it with the newest major
- B. Require sudo before every execution
- C. Run its local executable without a separate global install
- D. Run only JavaScript files you wrote yourself

Correct answer: C

Modern npx uses npm exec behavior. Matching local packages can provide the requested executable.

- Why not A: A matching local package can be reused.
- Why not B: Global administrator access is not inherently required.
- Why not D: npx can run executables supplied by packages.

### Question 2

A requested CLI package is missing locally. Where can npm exec make it available?

- A. Only in a permanent global installation
- B. Inside your source file as copied code
- C. It cannot run any package missing from local dependencies
- D. In an npm cache folder added to the command's PATH

Correct answer: D

Missing requested packages can be fetched into npm's cache. The cache may remain for reuse; execution does not promise automatic deletion afterward.

- Why not A: A global install is not required for this execution path.
- Why not B: npm exec does not insert the package into source files.
- Why not C: Fetching a missing requested package is supported.

### Question 3

Which habit makes an npx invocation more deliberate?

- A. Check the package name and specify the intended version
- B. Assume a familiar-looking name proves the package is safe
- C. Assume cached code cannot be malicious
- D. Use --yes as proof that npm has reviewed the code

Correct answer: A

Executing a package runs code. Verify the name/source and choose a version intentionally; npx is not a security sandbox or an always-latest guarantee.

- Why not B: Typos and impersonation can target package names.
- Why not C: Caching does not establish trustworthiness.
- Why not D: That option controls prompting, not a security review.

## Remember this

- `npx` executes Node command-line tools.
- It prioritizes locally installed packages (`node_modules/.bin`).
- Missing requested packages can be fetched into a reusable npm cache; they are not guaranteed to be deleted after execution.
- It eliminates the need for global installations of scaffolding tools (like `create-vite`).
