Skip to the solution
EasyBackend7 min read

What is npx and how does it differ from a global install?

646 views

The problem

Question presented to candidate: "A teammate suggests "just npm install -g this-tool" to run a one-off CLI, but you'd rather not pollute your global npm setup or worry about version drift across machines. What's the alternative, and what does it actually do differently?"

What a strong answer should cover:

  • npx runs a package's binary β€” it does not, by itself, require installing anything globally or persistently. Given a package already present as a local project dependency, npx <name> resolves and runs that local copy directly; given a package not present locally, npx fetches and runs it in a temporary cache, without adding a lasting global install.
  • πŸ“Œ Verified, not assumed: in a real project with a single bin entry declared in its own package.json (no separate dependency install even needed for this specific case), npx my-local-cli genuinely ran the local project's own script β€” confirmed by its real, actual resolved file path in the output β€” while a real npm ls -g immediately afterward genuinely showed nothing installed globally.
  • The core problem this solves, precisely matching the prompt: a global install (npm install -g) is persistent and shared system-wide β€” every project on the machine sees the identical global version, which genuinely causes real version drift between projects/machines/CI (one project needing an older CLI version than another, or a CI environment lacking whatever was manually installed globally on a developer's laptop). npx sidesteps this by preferring the project-local, version-pinned copy (declared in that project's own package.json/lockfile) β€” reproducible per-project, per-clone, per-CI-run, with no manual global setup step at all.
  • A precise answer names the project-local-first resolution as the single most important behavior: if a CLI is already listed as a project dependency (even a devDependency), npx <name> finds and runs that exact locally-installed, version-locked copy rather than reaching out anywhere else β€” this is different from, and safer than, assuming npx always means "download something fresh from the registry."
  • The honest scope: for a package genuinely not present locally at all, npx does reach out to the registry and runs a temporarily-cached copy β€” this is real, useful for a genuine one-off tool, but it is not magic offline resolution; it still needs registry/network access for that specific case, unlike the verified local-resolution case which needs none.

Clarifying questions expected:

  • "Is this tool something the project genuinely depends on repeatedly (belongs as a real devDependency, run via npx), or a genuine one-off never needed again?" β€” shapes whether it belongs in package.json at all versus an ad hoc npx run.
  • "Does this need to work identically and reproducibly across CI, without relying on anything manually pre-installed on a given machine?" β€” the core reason to prefer npx-of-a-local-dependency over a global install in the first place.

Code / implementation expected: Yes β€” a real project resolving and running its own local binary via npx, with a real, immediately-checked confirmation that nothing was installed globally, is the concrete, convincing proof of exactly what npx does and does not do.

  • nodejs
  • npm
  • npx
  • tooling

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

7 min read Β· 7 sections

Read the code

A real project with a local bin: npx resolves and runs it directly, with zero global install
# package.json
# { "name": "npx-demo", "bin": { "my-local-cli": "./cli.js" } }
#
# cli.js:
# #!/usr/bin/env node
# console.log("my-local-cli ran from:", __filename);

$ npm install
$ npx my-local-cli
my-local-cli ran from: C:\...\q5-npx\cli.js

# confirm nothing was installed globally, immediately afterward:
$ npm ls -g my-local-cli
C:\Users\...\AppData\Roaming\npm
`-- (empty)

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 14 of 152 in Node.js

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Node.js questions
Up nextEasy

What does NODE_ENV=production actually change and why does it matter?

Continue