Using Bun as a Package Manager
After the release of Bun on September 8th, 2023, I saw a lot of buzz about it on YouTube and Twitter. A week later, I decided to give it a try as my package manager, and I immediately replaced pnpm with bun.
Bun is an all-in-one JavaScript runtime and toolkit designed for speed. It comes with a bundler, test runner, and a Node.js-compatible package manager.
Let's explore how to use Bun as a replacement for npm, yarn, or pnpm in a typical Laravel application.
Installing Bun
Installing Bun is straightforward. If you’re on Mac or Linux, simply run this bash command:
If you are on Mac and prefer Homebrew, you can install it with:
On Windows, you can use the following command:
Once installed, verify the installation by running bun --version in your terminal.
Using Bun
Now, let's use Bun as a replacement for npm, yarn, or pnpm.
To test Bun's speed, create a new Laravel application with Vue and Inertia. To install the dependencies, run:
Installing dependencies on this fresh Laravel project with no cache took 2.30 seconds on my device. Now, let's try installing with cache:
This time, it took only 74 milliseconds to install all the dependencies with cache. Impressive, right?
Installing Dependencies with Bun
You can use Bun to install dependencies just like you did with yarn or npm. To install a new dependency, run:
To install a dependency as a dev-dependency, use:
Adding new dependencies is very fast as well.
Removing Dependencies
To uninstall existing dependencies, use the rm or remove command:
Running Scripts
In your Laravel projects, you likely used npm run dev or yarn run dev to run Vite. Bun provides the same options for running package.json scripts.
To run your Laravel Vite dev server, you can run:
You can even shorten it by dropping the run if the script name is not a reserved Bun action name:
Bun is a powerful tool that significantly speeds up dependency management and script execution in Node.js projects. Give it a try, and you might find yourself switching to Bun for good!
Conclusion
Bun is a powerful tool that significantly speeds up dependency management and script execution in Node.js projects. Give it a try, and you might find yourself switching to Bun for good!
Your feedback is valuable to me, so if you'd like to see more content like this in the future, please don't hesitate to let me know.
Keep coding, keep exploring, and keep inspiring. 🐼