Skip to content

fix(perf): run an exact version from the npx cache without a registry check - #10325

Open
azchohfi wants to merge 3 commits into
npm:latestfrom
azchohfi:perf/npx-exact-version-cache-hit
Open

azchohfi wants to merge 3 commits into
npm:latestfrom
azchohfi:perf/npx-exact-version-cache-hit

Conversation

@azchohfi

Copy link
Copy Markdown

Fixes #10324
Refs #7295

Depends on #10096, which this branch includes as its first commit. Only the second commit is new here.

npx create-vite@9.2.1 my-app with create-vite 9.2.1 already in the npx cache still loads the local tree with Arborist, fetches the create-vite packument (getManifest passes preferOnline: true), loads the npx cache tree, and only then runs the bin it already had.
That's 565 modules and one registry round trip on npm 12.2.3.
npm create vite@9.2.1 does the same, and also loads Arborist at start-up through update-workspaces in lib/commands/init.js.

When every package is a registry spec for an exact version, the answer can't change.
So before any of that, exec now checks the npx cache directory for those packages.
If each one is installed there at exactly that version, and none of them is in the local project or, for npx <pkg>, in the global prefix, it runs the bin from there.
It reads a few package.json files with @npmcli/package-json, the way Arborist does, and holds the npx cache lock while it reads, so it doesn't see a half-finished install from another npx.

Everything else takes the full lookup as before

  • ranges, tags, aliases, git, file and directory specs
  • a version that isn't in the cache, or a cache entry with another version
  • a package that is in node_modules, or installed globally (only checked for npx <pkg>, as before)
  • --prefer-online, which asks for the registry check
  • before or min-release-age, which need the packument's publish times

npm init <initializer> now requires init-package-json and update-workspaces where they're used, as the other commands already do for Arborist.

With #10096, npx and npm create load about 140 modules instead of 565 and 581, and make no registry request.
With a warm cache npx create-vite@9.2.1 gets about 1.85x faster on Windows and 2.7x on Linux, and npm create vite@9.2.1 about 1.75x on both.

warm cache, --prefer-offline, Node.js 26.7.0 Windows Linux (WSL2)
npx create-vite@9.2.1 my-app --template react, first version of this change on npm 11.19.0, two runs 1.82x (1.74-1.86), 1.87x (1.73-2.07) 2.77x, 2.63x
same, this branch on npm 12.2.3 1.89x (1.69-2.16) 2.85x (2.47-3.22)
npm create vite@9.2.1 my-app -- --template react, npm 11.19.0, two runs 1.74x (1.67-1.79), 1.75x (1.65-1.81) 1.73x, 1.75x
same, this branch on npm 12.2.3 1.64x (1.32-1.91) 1.74x (1.60-1.97)
modules loaded on 12.2.3, npx / npm create 565 → 141 / 581 → 149 same

Ratios are unpatched over patched, medians of 10 interleaved paired runs each with 95% intervals, on a busy shared machine, so the ratios mean more than the milliseconds.
Without --prefer-offline the unpatched run also waits for the registry (1.2 s here), and the patched run doesn't.
#10096 on its own doesn't change this case (1.03x, 0.89-1.10), because the full lookup loads Arborist anyway.

One trade-off, stated plainly.
A cached exact version now runs without asking the registry whether it still exists.
If it gets unpublished or blocked later, it keeps running from the npx cache until the cache is cleared, the same as a copy in node_modules would.
#10295 makes the same trade for the packument.
--prefer-online keeps the old behaviour.
The docs for npm exec say this in "A note on caching".

Tests are in workspaces/libnpmexec/test/npx-cache-hit.js.
They cover the hit (no registry request and no Arborist, with and without --package), and that --prefer-online, before, a version that isn't cached, @^1.0.0, @latest, a cache entry with a different version, a copy in the project and a global copy all still take the full lookup.
The hit tests fail without the change, and the project and global ones fail if the fast path skips those checks.
Coverage stays at 100%.

Related

azchohfi and others added 2 commits November 20, 2026 10:55
`npm exec` / `npx` required `@npmcli/arborist` and `pacote` at the top of
libnpmexec, so running a bin that is already in `node_modules/.bin`
loaded about 420 modules (fetchers, make-fetch-happen, sigstore, tuf-js,
cacache, tar, ...) that this path never uses.

- require arborist and pacote on first use
- read the local package.json for `hasPkgBin` with
  `@npmcli/package-json` directly, which is what pacote's DirFetcher
  does for a directory spec
- with `--call` and no `--package`, run the command without reading the
  local tree, whose result was never used

`npx <local bin>` and `npx -c <cmd>` now load 140 modules instead of 560
before spawning the command.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
… check

`npx foo@1.2.3` with foo@1.2.3 already in the npx cache still loads the
local tree with Arborist, revalidates the packument with the registry
(getManifest uses preferOnline) and loads the npx cache tree, only to run
the same bin from the same directory.

When every package is a registry spec for an exact version, that version
is the one installed in its npx cache directory, and the package is not in
the local project (or, for `npx <pkg>`, in the global prefix), run it from
there directly. The check reads package.json files and holds the npx cache
lock while it does, so it does not race an install into the same
directory. Ranges, tags, aliases, git and directory specs, `--prefer-online`
and a `before`/`min-release-age` cutoff still take the full lookup.

`npm init <initializer>` also stops loading init-package-json and
update-workspaces (and through it Arborist) when it only runs
`npm exec`.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@azchohfi
azchohfi requested a review from a team as a code owner November 21, 2026 01:20

@ychampion ychampion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reproduced a workspace regression on 9c9c627 with a warm npx cache and npm install --install-strategy=nested. The workspace has the exact requested CLI version and its optional peer installed under packages/app/node_modules.

On 787f152 and the lazy-load parent 834a00f, both npm exec -w app -- tool@1.0.0 and npm exec -w app --package=tool@1.0.0 -- tool run the workspace copy and load the peer. Running those forms from inside the workspace gives the same result. On this head, all four executions start the same version from _npx and the peer is missing. An ordinary direct project install still works.

findInNpxCache checks the root prefix's node_modules. The cached bin directory then takes precedence over the workspace's bin directory. I tried skipping the fast path when resolve(runPath) !== resolve(path). That restored the workspace binary and peer in all four cases, while the standalone warm-cache case still made no registry requests and loaded 141 modules. A regression test can assert the binary's actual path or the presence of the workspace peer.

The npx cache fast path only looked for the package in the project root's
node_modules. With `npm exec -w <ws>`, or from inside a workspace, the full
lookup also finds the copy in the workspace's node_modules and runs it from
the workspace's bin directory, where its peers resolve. The fast path ran the
npx cache copy instead.

Look in node_modules of every directory from runPath up to the project root,
the same directories whose bins come first on the PATH, and take the full
lookup if any of them has the package.

Reported by @ychampion.
@azchohfi

Copy link
Copy Markdown
Author

Thanks for catching this @ychampion. Fixed in 79458ec. I didn't go with skipping whenever runPath differs, since that would turn it off for any run from a subfolder. It now looks for the package in node_modules from runPath up to the project root, and takes the normal path if it finds it there. Your four cases run the workspace copy with the peer again, and there's a test for it now. Standalone warm cache is still 0 requests and 141 modules.

One thing I left as is. From the root, if only a workspace has the package, plain npm finds it but can't run it because it's not on the PATH, and this branch runs the cached one instead. Can change that if you think it should match.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] npx <pkg>@<exact version> loads Arborist and asks the registry when the npx cache already has it

2 participants