If you’re using Bun as your JavaScript package manager, you might want to replace npx with bunx. However, some tools expect npx to exist and won’t recognize a simple alias. Here’s how to work around that issue.
Why Replace npx with bunx?
Bun is a faster alternative to npm, and bunx serves the same purpose as npx, allowing you to run Node.js packages without installing them globally.
For example, instead of:
npx create-next-app@latest my-app
You can use:
bunx create-next-app@latest my-app
However, some tools strictly require npx. If you simply alias it in your shell configuration:
alias npx="bunx"
It will work in an interactive shell but may fail in automated scripts or commands like uv run.
Workaround: Create a Fake npx Script
A more reliable way is to create a lightweight script that redirects npx calls to bunx.
Steps:
-
Create a custom script for
npx:mkdir -p ~/.local/bin echo '#!/bin/sh' %3E ~/.local/bin/npx echo 'exec bunx "$@"' >> ~/.local/bin/npx chmod +x ~/.local/bin/npx -
Add
~/.local/binto your systemPATH(if not already included):export PATH="$HOME/.local/bin:$PATH"Add this line to your
~/.bashrc,~/.zshrc, or~/.profileto make it permanent. -
Restart your terminal or run:
source ~/.zshrc # or source ~/.bashrc
Now, any command expecting npx will automatically use bunx instead.
Conclusion
Using bunx instead of npx can speed up development, but some tools depend on npx existing in the system. This simple script method allows you to replace npx with bunx without breaking compatibility with tools like uv run or other automated scripts.>)