Bun: The Lightning-Fast Alternative to NPM, Yarn, and pnpm
What is Bun?
Bun is a blazing-fast JavaScript all-in-one toolkit. It serves as a runtime (a drop-in replacement that’s significantly faster than Node.js), a test runner, and even a package manager – which is our focus today.
Unlike Node.js or Deno, Bun is built on top of WebKit‘s JavaScript engine (JavaScriptCore). WebKit forms the foundation for Mobile Safari on Apple’s mobile platforms, as well as Safari for Mac.
Why switch from NPM, pnpm, or Yarn to Bun?
You might wonder why you should invest time in switching to Bun instead of sticking with your familiar Node.js runtime. The answer lies in Bun’s incredible performance.
When you test Bun, you’ll immediately notice how astonishingly faster it is compared to Node.js. We’re talking up to 30 times faster! This speed boost translates to:
- Lightning-fast installation of front-end dependencies.
- Significantly quicker asset compilation.
- Accelerated continuous integration environments, as installing and compiling front-end dependencies takes a fraction of the time.
How to install Bun on macOS using Homebrew
Installing Bun on macOS is a breeze. Simply add the new source and install Bun using these two commands:
brew tap oven-sh/bun brew install bun
How to install on Linux and WSL
Installing Bun on Linux is just as straightforward as on macOS. Run this single command:
curl -fsSL https://bun.sh/install | bash
Linux users should ensure the unzip
package is installed first. It’s also recommended to run kernel version 5.1 or higher, with version 5.6+ being the optimal choice for the best experience.
How to install Bun on Windows
Currently, Bun’s package manager capabilities are not available natively for Windows. However, this shouldn’t be an issue if you’re using Windows Subsystem for Linux (WSL).
There’s an experimental version available for Windows, but it’s not recommended for production use at this time.
Preparing to replace NPM, Yarn, or pnpm with Bun
The beauty of package managers is that you’re not locked into a specific one. This flexibility is great news for Bun!
If you’re transitioning from NPM or pnpm, you’ll need to remove their lock files as Bun uses its own lock file called bun.lockb by default.
For NPM users:
rm package-lock.json
For pnpm users:
rm pnpm-lock.yaml
And for Yarn users:
rm yarn.lock
Installing dependencies with Bun’s package management
To install your dependencies using Bun, simply run:
bun install
You’ll be amazed at how quickly it completes the task!
If you encounter any issues and need to bypass the cache, use:
bun install --no-cache
For more detailed information and additional options, consult the official documentation of the bun install
command.
Adding packages with Bun
Adding packages with Bun is a breeze using the bun add
command. You’ll appreciate its incredible speed here as well.
Here’s an example of adding multiple packages:
bun add tailwindcss autoprefixer postcss
For more information and options, refer to the official documentation of the bun add
command.
Removing packages with Bun
Removing packages with Bun is just as simple, using the bun remove
command. The speed here is equally impressive.
Let’s use Axios as an example, since we can now use the native Fetch API:
bun remove axios
For additional details and options, check out the official documentation of the bun remove
command.
Running scripts with Bun
Bun seamlessly integrates into your existing workflow. Run the scripts defined in your package.json file just like before, using bun run
.
For instance, if you’re using Vite for your compilation process:
bun run dev
For more information and options, consult the official documentation of the bun run
command.
Bun’s video presentation
Get a visual introduction to Bun’s capabilities with this official video: