An introduction to Bun.sh (Another JavaScript Runtime)
This article was originally published here on September 16, 2022
Bun.sh an another Javascript runtime. We already had and covered Deno and Node.JS multiple times. It seems like there is going to be no dearth for Javascript runtime in near future.
In my honest opinion, these many runtimes only accelerated the development of ECMAScript standards and thus made JavaScript as a programming language better. And that’s the only good thing I see. (Does anyone; remember Javascript before 2016 or ES6)
As a community, we should have tried to make Node.JS better instead of coming up with runtime every time we find faults in Node.JS. There is no doubt that Node.JS have flaws; so does every other programming language that existed on the globe. That’s enough of my rants. Let’s Jump in
Introduction
As per Bun.sh
Bun is a fast all-in-one JavaScript runtime
The objective of Bun is to provide the ability to bundle, transpile, install, and run JavaScript & TypeScript projects — all in Bun. Bun is a new JavaScript runtime with a native bundler, transpiler, task runner, and npm client built-in.
That indicates that Bun may prove to be a drop-in replacement for Node.JS and other Javascript runtime code, that’s good news as the same can not be said for Deno
Bun was built from scratch keeping the following points in mind
- Start fast (it has the edge in mind).
- Performance (extending JavaScriptCore, the engine).
- Being a great and complete tool (bundler, transpiler, package manager).
It is claimed that Bun implements thousands of Node.JS API and at the same time modern WebAPI.
So in the same breath, it is claimed that they backported all Node.JS API and WebAPI, but with a different JavaScript engine (Bun uses JavaScriptCore engine) to improve performance. So far it looks like just a replacement of Javascript engine (for performance enhancement) and few other goodies (i.e bundle, transpile and run)
Installation
It’s been a lot of ranting, let’s dive in. To install Bun run the following curl
command to download and install Bun
curl -fsSL https://bun.sh/install | bash
First Web server
As claimed; the following code would look familiar to Node.JS developers.
// server.js
export default {
port: 4000,
fetch(request) {
return new Response("Welcome to our World");
},
};
and then run it with
bun run server.js
CLI Commands
Bun implements most of NPM commands we are used to; just replace npm
with bun
and there you go
Running a script
To run a JavaScript file, you just need to use bun run <filepath>.js
or even TypeScript files.
Installing packages
To install JavaScript packages (including npm packages). Just replace npm install <package>
with bun install <package>
Running Tests
Bun has inbuilt CLI for running tests. One can run tests using bun wiptest
Upgrading Bun
To upgrade Bun; bun provides bun upgrade
command. One can also choose to run canary builds by using --canary
flag to upgrade command
Other Important facts
Since Bun is drop-in replacement for existing JavaScript runtime; it uses package.json
for package management. To add/remove packages from package.json
; use
bun add <package>
bun remove <package>
Bun also loads .env
files atomatically, no need to load it explicitly using require("dotenv").config()
Bun auto transpile .ts
and .tsx
file on import and also reads our tsconfig.json
Bun comes with type support using bun-types
Note: As of writing this article Bun is still in beta. So be aware before using this. One can use it for understand it better and play around with it
About The Author
I am Pankaj Baagwan, a System Design Architect. A Computer Scientist by heart, process enthusiast, and open source author/contributor/writer. Advocates Karma. Love working with cutting edge, fascinating, open source technologies.
Stay tuned <3. Signing off for RAAM