Bun: The Lightning-Fast Alternative to NPM, Yarn, and pnpm

Bun: The Lightning-Fast Alternative to NPM, Yarn, and pnpm

Published
Sep 11, 2023
Written by
Benjamin Crozat
0
comments
4 minutes
read

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:

  1. Lightning-fast installation of front-end dependencies.
  2. Significantly quicker asset compilation.
  3. 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.

bun install in action.

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.

bun add in action.

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:

https://www.youtube.com/watch?v=BsnCpESUEqM

About Benjamin Crozat
Benjamin Crozat

Hi! I’m from the South of France and I’ve been a self-taught web developer since 2006. When I started learning PHP and JavaScript, PHP 4 was still widely used, Internet Explorer 6 ruled the world, and we used DHTML to add falling snow on websites.

Being able to educate myself for free on the web changed my life for the better. Giving back to the community was a natural direction in my career and I truly enjoy it.

Therefore, I decided to take action:

  1. I launched this blog in September 2022 with the goal to be in everyone’s Google search. I get more than tens of thousands of monthly clicks from it and even more visits overall (my analytics dashboard is public by the way).
  2. I also started growing my X (formerly Twitter) account at the same time, which has now over 7,000 followers.
  3. All the content I write is free thanks to my sponsors.

I also want to be completely free with my time and make a living with my own products. In April 2024, I launched Nobinge, a tool to summarize and chat with your content, including YouTube videos.

Believe me, I’m just getting started!

0 comments

You need to be signed in to comment this post.
Sign in with GitHub