日本語

How Bun’s bunx Differs from npx and How Fast It Runs

​​Bun​CLI,Node.js,npm

npx and bunx both run commands from npm packages without a separate install step. In my test environment, bunx finished faster for the same packages. For prettier, which was already installed in the project, npx took 320.5 ms and bunx took 86.7 ms. However, unless you specify otherwise, bunx also runs commands written for Node.js with Node.js.

When you want to run a formatter or a scaffolding tool once without installing it globally, you can use bunx in place of npx. There are differences from npx, though. For example, bunx installs without asking for confirmation when run from a terminal, and it behaves differently when you specify a version.

Note

The test environment was Ubuntu 24.04 (x86_64, 4 CPUs), Node.js 22.23.4, npm 10.9.9 (npx 10.9.9 as well), and Bun 1.4.4. I used prettier 3.10.1 and cowsay 1.6.0, fetched from the public npm registry. To measure timing, I moved the npm and Bun caches to a working folder and turned off npm's update notifier. When the cache was empty, packages were fetched from the registry every time, so the variation between runs was large. I did not test Windows or macOS, company registries or proxy settings, or the once-a-day update check that bunx performs.

How I measured startup time and the results

I ran --version for prettier in three states and measured the time from starting the command until it exited. The three states were: prettier installed in the project's node_modules, not installed but still in the cache, and cache cleared. I took the median of 10 runs for the first two states and 7 runs for the cache-cleared state.

Time from prettier start to exit. Installed in the project: bunx 86.7 ms, npx 320.5 ms. Cached: bunx 80.7 ms, npx 657.4 ms. No cache: bunx 196.3 ms, npx 1126.8 ms
Time until prettier –version exits (median)
State npx bunx
Installed in the project 320.5 ms 86.7 ms
Not installed (in cache) 657.4 ms 80.7 ms
Not installed (no cache) 1126.8 ms 196.3 ms

For comparison, I also timed running ./node_modules/.bin/prettier --version directly. It took 92.8 ms. The 86.7 ms for bunx is almost the same as the direct run. npx took more than 200 ms longer than the direct run.

The official bunx documentation says that for packages installed in the project, bunx is about 100 times faster than npx. In my prettier test, the difference was about 3.7 times. As long as prettier runs on Node.js, calling it through bunx does not reduce the time much below the direct run's 92.8 ms.

With a cache, npx still queried the registry

I ran a package that was not installed in the project once so it would be cached, then ran it again. bunx took 80.7 ms, almost the same as when the package was installed in the project. npx took 657.4 ms, about twice as long as when the package was installed in the project.

I ran cowsay with npx and --loglevel=http to see the network activity.

text
$ npx -y --loglevel=http cowsay@1.6.0 hi 2>&1 | grep -E '^npm http' | sed -E 's/ [0-9]+ms//'
npm http fetch GET 200 https://registry.npmjs.org/cowsay (cache revalidated)

Even with a version specified and the package already cached, npx asked the registry for package information. However, when I added --offline to prevent network access, prettier still took 631.4 ms. Blocking the network reduced the time by only 26 ms, so most of the time is spent on something other than network access.

According to the official bunx documentation, bunx checks the registry for newer versions once a day, and it checks every time when you use @latest.

When I cleared the cache every time, the difference depended on the package. prettier, which has no dependencies, showed a large gap: 1126.8 ms with npx and 196.3 ms with bunx. cowsay, which fetches 29 packages including its dependencies, showed a smaller gap: 1876.4 ms with npx and 1362.1 ms with bunx. For bunx with cowsay, only the first of the 7 runs took 387.1 ms. The rest took 1.3 to 1.4 seconds.

When run from a terminal, only npx asks for confirmation

When you run a package that is not installed in the project, npx asks for confirmation before installing it if you are running from a terminal. When I answered n, it stopped without doing anything and exited with code 1.

text
$ npx prettier@3.10.1 --version
Need to install the following packages:
prettier@3.10.1
Ok to proceed? (y) n

npm error canceled

(A line showing the location of the npm log file follows.)

According to the official npx documentation, this prompt prevents you from installing a different package by mistake when you mistype a package name. Adding -y (--yes) installs without asking.

bunx did not ask for confirmation even when run from a terminal. It fetched the package and ran it right away.

text
$ bunx prettier@3.10.1 --version
🔍 Resolving [1/1]   🚚 prettier [4/3]   🔒 Saving lockfile... 3.10.1

(I removed the control characters from the progress display and put it on one line.)

Warning

If you mistype a package name, bunx will still fetch and run that package without asking. Check the package name before you run it.

When standard input was not a terminal, npx also did not ask for confirmation. It printed the warning npm warn exec The following package was not found and will be installed: cowsay@1.6.0, then installed and ran the package.

bunx can also run on Node.js

I created a command that only prints which runtime it ran on, and installed it in the project. The first line of the executable is #!/usr/bin/env node.

text
$ bunx runtime-check a b
process.version: v22.23.4
process.versions.bun: undefined
args: ["a","b"]

$ bunx --bun runtime-check a b
process.version: v26.3.0
process.versions.bun: 1.4.4
args: ["a","b"]

Plain bunx ran on Node.js 22. npx runtime-check a b produced the same output. With --bun, it ran on Bun. (With Bun 1.4.4, process.version returns v26.3.0.) The official bunx documentation also says that a command whose first line is #!/usr/bin/env node runs in a node process, and that --bun makes it run on Bun.

--bun must come before the command name. When I placed it after the command, as in bunx runtime-check --bun a, the command ran on Node.js, and --bun was passed to the command as an argument (args: ["--bun","a"]).

The first line of prettier's executable is also #!/usr/bin/env node. bunx --bun prettier --version took 62.0 ms, shorter than the 86.7 ms of bunx prettier --version, which ran on Node.js. However, you need to check for each tool whether a Node.js tool works the same way on Bun.

Options that work the same, and behavior that differs

When the command name differs from the package name, both tools use -p to specify the package. Running cowsay's cowthink with npx -p cowsay@1.6.0 cowthink hmm and with bunx -p cowsay@1.6.0 cowthink hmm produced the same output.

The behavior when you specify a version is slightly different. In a project with prettier 3.10.1 installed, I specified a different version, prettier@3.9.0. Both tools ignored the project's copy, fetched 3.9.0, and ran it. When I specified prettier@3.10.1, the same version as the project, npx printed 3.10.1 without an install warning. bunx fetched the package from the registry and then ran it. It showed Resolved, downloaded and extracted, and a prettier@3.10.1 folder appeared in the cache.

The documentation also describes this difference. According to the official npx documentation, npx uses the project's copy only when a versioned name matches the installed name and version. According to the official bunx documentation, when you specify a version, bunx does not use the project's copy.

To run only what is already available locally, without installing, use --no with npx and --no-install with bunx. When I specified a package that was not in the cache, both stopped with exit code 1.

text
$ npx --no cowsay@1.5.0 hi
npm error npx canceled due to missing packages and no YES option: ["cowsay@1.5.0"]

$ bunx --no-install cowsay@1.5.0 hi
error: Could not find an existing 'cowsay' binary to run. Stopping because --no-install was passed.

(After the npx message, a line showing the location of the log file follows.)

The older --no-install option for npx is treated as --no according to the npx documentation. When I tried it, it stopped with the same message as --no.

The location where the fetched packages are stored also differs. npx created a folder with a 16-character alphanumeric name, such as da76e343824a5421, under _npx/ in the npm cache folder. bunx created a folder named name@version, such as cowsay@1.6.0, under .bunx-<number>/ in Bun's global cache. According to the Bun global cache documentation, the global cache is usually located at ~/.bun/install/cache.

​Posted by 86