'bun' is a fast JavaScript runtime, package manager, bundler and test runner. It is intended to be a replacement for 'node' and 'npm' plus some additional features. It's home on the Internet is 'bun.sh'.

Bun provides some interesting features:

  • It replaces node and npm by bun.
  • While node is based on V8, bunis based on JavaScriptCore.
  • It can execute TypeScript files directly (no need for transpiling code to JS before).
  • It can serve as a test-runner and bundler.
  • It integrates perfectly with the existing node/npm- landscape.
  • And it is known to be faster than node with reduced memory consumption.

Enough features, to give it a trial. We started to monitor bun-progress in March 2024 with an initial version of this article. In between a lot of progress has been made and we are looking forward to the next steps.We updated the article in Fall 2024.

Motivation

We have an internal app using Puppeteer to aggregate screenshots of Web-Apps. It is written in TypeScript and catches images of sites in headless browser mode.

So it is the a perfect candidate for a trial with bun.

We tested with 'bun' version '1.0.33' in March 2024 initially, and later we updated this article based on 'bun' version '1.1.33'.

Installation

The installation of 'bun' is quite simple. Just run the following command in your terminal:

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

This will install 'bun' on your system. You can check the installation by running 'bun --version'.

Upgrading an existing version of 'bun'

If you have an existing version of 'bun' installed and want to upgrade to the latest version, you can run the following command:

bun upgrade

This will upgrade 'bun' to the latest version and a number of comments are displayed on the console:

Bun v1.1.34 is out! You're on v1.1.33
[2.62s] Upgraded.

Welcome to Bun v1.1.34!

What's new in Bun v1.1.34:

    https://bun.sh/blog/release-notes/bun-v1.1.34

Report any bugs:

    https://github.com/oven-sh/bun/issues

Commit log:

    https://github.com/oven-sh/bun/compare/bun-v1.1.33...bun-v1.1.34

'bun' project creation

Creating a project with 'bun' is quite simple and very similar to project creation with 'npm'. Just run the following command in your terminal:

mkdir my-project
cd my-project
bun init

'bun' will ask you some questions - like 'npm'. At the end 'bun' will create a new project with the following structure:

  • bun.lockb
  • .gitignore
  • index.ts
  • node_modules
  • package.json
  • README.md
  • tsconfig.json

Installing dependencies

You can install dependencies with 'bun' using the following command:

bun add puppeteer

This will install the 'puppeteer' package and add it to the 'dependencies' section of the 'package.json' file.

Using Puppeteer with 'bun'

By adding the 'puppeteer' package to your project, you can use it in your code. Here is an example of how you can use 'puppeteer' with 'bun':

import * as puppeteer from "puppeteer";

const browser = await puppeteer.launch({
  headless: true,
});

const page = await browser.newPage();
await page.emulate(puppeteer.KnownDevices["iPad Mini"]);
await page.goto("https://advenage.com", {
  waitUntil: "networkidle0",
});
await page.screenshot({ path: "demo.png" });
await page.close();
await browser.close();

The code above will open a headless browser, emulate an iPad Mini, navigate to 'https://advenage.com', take a screenshot of the page and save it as 'demo.png'. The source code is written in TypeScript and can be executed with 'bun' directly.

Save the code above in a file called 'snapshot.ts'.

If you have saved the code above in a file called 'snapshot.ts', you can run it with 'bun' using the following command:

bun snapshot.ts

And finally we got a screenshot of of the page https://advenage.com in the PNG-file demo.png.

Summary

In the case of running 'Puppeteer' applications with 'bun' instead of 'node' the results convinced us to use 'bun' in more complex scenarios in the future as well. Especially seamless execution of TypeScript-code simplifies things in "glue"-applications.

We also have examples, where bun was no suitable to replace node.

However, bun is a promising, fresh and new approach.