How to replace npm/yarn/* with bun in a PHP/Laravel Project

How to replace npm/yarn/* with bun in a PHP/Laravel Project

Introduction

Bun is a fast JavaScript all-in-one toolkit that lets us develop, test, run, and bundle JavaScript & TypeScript projects. It is an all-in-one JavaScript runtime & toolkit designed for speed, complete with a bundler, test runner, and Node.js-compatible package manager.


Why Switch from Npm/pnpm/yarn to bun?

There's a reason why this new runtime library has been in talks since it launched. And, we can feel the performance difference while trying the basic commands like bun install and bun run dev

As per the official claims, it is 30x faster than npm, 17x faster than pnpm, and 33x faster than yarn.


Installing bun

Linux Users

The unzip package is required to install Bun. Use sudo apt install unzipto install the unzip package. Kernel version 5.6 or higher is strongly recommended, but the minimum is 5.1. Use uname -r to check the Kernel version.

Installation

Once this is done, bun can be installed using curl

curl -fsSL https://bun.sh/install | bash

For macOS: If you prefer to install bun with brew:

brew tap oven-sh/bun
brew install bun

For Windows: Bun provides a limited, experimental native build for Windows. At the moment, only the Bun runtime is supported.


Replacing npm/yarn/* with bun in Laravel

Let's start by removing the respective lock files since the bun uses its own bun.lockb file.

If you're using npm:

rm package-lock.json

And pnpm.lock and yarn.lock if you're using pnpm and yarn respectively.

Installing frontend dependencies with bun

Just replace npm with bun and you're good with most of the commands.

So how fast was it? You probably didn't expect it to be so fast right?

In the screenshot attached above (this project uses the inertia stack, with Svelte as a frontend), it literally installed all the dependencies in just 312ms

Adding and removing packages with bun is also very easy, with bun add *** and bun remove ***

Running the scripts using bun

You can run the scripts defined in your package.json file just like before using bun run followed by the command, like so:

// to run the dev server
bun run dev
// to build the assets
bun run build

🔥 New Course Alert
★★★★★5/5

Nuxt 3 Authentication with Laravel Sanctum:A Practical Guide

Learn how to build a full authentication boilerplate with Nuxt, Sanctum and Fortify.

30-Day Money Back
20+ Students
Limited Time
80% OFF
Enroll Now
M
Marcus M.
★★★★★

Explains everything nice and clear. Very beginner friendly.