Bun with Next.js & Vercel

Bun with Next.js & Vercel

Tags
Bun
Web Dev
Vercel
Next.js
Published
September 13, 2023
Author
Kalen Wallin
api_v1_role
api_v1_company
api_v1_type
api_v1_category
api_v1_year
api_v1_cover
api_v2_order
api_v3_link
If you’ve paid any attention to the world of JavaScript web development lately, you’ll know that Vanilla JavaScript and Bun are taking the community by storm, with major projects, like Turbo and Svelte, switching from TypeScript to JavaScript with JSDoc for IDE-level typing. As someone who is currently learning TypeScript, I now am even more lost on whether I should continue or return to vanilla JavaScript.
However, I’m here to talk about Bun, the JavaScript runtime and package manager that is a direct competitor to Node.js. Bun’s package manager is 33x faster than Yarn, 29x faster than npm, and 17x faster than pnpm. Although Bun’s runtime is advertised to be faster as well, Next.js currently only supports Bun as a package manager so that’s all I’ve used so far.
Here are my results from switching from npm to bun package manager with Next.js on Vercel:
$ npm install --legacy-peer-deps
$ npm install --legacy-peer-deps
$ bun install
$ bun install
 
*I use the legacy-peer-deps flag to resolve some janky React dependencies.
Next.js Build
$ npm install
$ bun install
Package Quantity
457
456
Time (s)
32
2.73
Switching from npm to bun resulted in an 11.72x speed increase.

Getting Started with Bun Package Manager

At the time of writing, Bun is available on Mac, Linux, and WSL. They have limited Windows support, so I’d recommend just using WSL. The installation for all operating systems can be found here. If you’re using WSL, then here’s my quick setup guide:

Bun Package Manager - Quick Setup with WSL

Install unzip and curl first
sudo apt-get install unzip curl
Then install bun
curl -fsSL https://bun.sh/install | bash
To get started, run:
source /$HOME/$USER/.bashrc bun --help
Upgrade bun
bun upgrade
Install packages with bun
bun install
This will create a bun.lockb file in your root directory. Vercel will pick up on this and automatically run bun install instead of npm install when building your Next.js website. You can remove the package-lock.json file to avoid any conflicts with npm.

Summary

Today you learned how to swap out npm for bun to increase package installation and build times for Next.js on Vercel. I was able to get a speed increase of 11.72x with bun compared to npm. What speed increase will you get? Let me know on LinkedIn or Twitter/X!