How to setup Bun - JavaScript runtime

So I’m finally going to make the switch to the JavaScript runtime bun.js after using node package manager (npm) for so long.

bun-1.png

Setup

To install bun on macOS / Linux run the following command.

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

To make the bun command available add the following to the .zshrc file

export BUN_INSTALL="$HOME/.bun" 
export PATH="$BUN_INSTALL/bin:$PATH"

Run bun in the command line to make sure it’s working.

bun

Running a file

Create a file named index.tsx with the following content inside of the file.

const server = Bun.serve({
  port: 3000,
  fetch(request) {
    return new Response("Welcome to Bun!");
  },
});

console.log(`Listening on localhost:${server.port}`);

Then use bun to run the file.

bun index.tsx

Go to http://localhost:3000 in the web browser and you should see the string output.

bun-2a.png

More commands

Use watch flag to check for changes and re-run.

bun --watch index.ts

Extra info

Use the main website to check for guides on how to use bun with frameworks and libraries.

http://bun.sh