Bun.sh: JavaScript Runtime Tool | Node.js Alternative

0

 

Introduction:

In today’s ever-changing world, manufacturers are constantly looking for tools and technologies that can streamline their operations and make their code more efficient. Node.js has been a game-changer in this regard, allowing developers to use JavaScript on the server side. But what if there was a tool that provided the same experience with different features? Install Bun.sh, a JavaScript runtime tool ready to change the way you work with JavaScript.
bun.sh all in one javascript runtime


What is Bun?

As per official website of bun.sh, Bun is all in one toolkit for javascript and typescript apps. It is written in Zig which is lightweight and powerful language to write low level programming. 

Well, Bun has this special powers, which makes it faster than javascript Node js in terms of startup time and memory usage.

Bun is designed as a drop-in replacement for Node.js. It basically uses Node.js and hundreds of Web APIs, including fs, path, Buffer and more. Bun aims to automate most of the world’s server-side JavaScript and provide tools to improve performance, reduce complexity, and increase developer productivity.

Some of the notable feature of Bun.sh are as below:
  • Node js compatibility
  • Support for web API
  • Inbuilt support for JSX
  • First class citizen treatment for Type Script
  • No more complicated rules to manage Modules

So lets dive into code and see how we can use it as Node.js alternative.

Step 1: Install bun runtime

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

It will automatically add it in the path variable and its ready to use.
Check if its correctly working by executing below command:

bun --help

Step 2: Create your javascript app using bun

Let's create a new javascript application using the bun CLI.  Create your app directory named hello-server.
mkdir hello-server
cd hello-server
create your project as below:
 [wranto@ hello-server]$ bun init
        
  bun init helps you get started with a minimal project and tries to guess sensible defaults. Press ^C
        anytime to quit.
   package name (hello-server): 
      entry point (index.ts): 
   Done! A package.json file was saved in the current directory.
       + index.ts
       + .gitignore
       + tsconfig.json (for editor auto-complete)
       + README.md
   To get started, run:
       bun run index.ts
        
Once the project is generated, import it into your favourite IDE. I will be using VSCode.

Project Structure

Step 3: Create a bun server

The syntax is similar to node js. Add below code in index.ts to create server in bun. To make it more fun, i am using filglet which just converts your strings into ASCII art.
import figlet from "figlet";

const server = Bun.serve({
  port: 3000,
  fetch(req) {
    const body = figlet.textSync("Hello Bun!");
    return new Response(body);
  },
});

console.log(`Listening on http://localhost:${server.port} ...`);
To execute the project use below command:
bun run index.ts

It will start the server on port 3000 and will see the response as below:



Congratulations! You have successfully created your first bun.sh app similar to Node.js

4. Executing bun project via script:

As you all know, you can configure build, run script in packge.json for node js. Similar way you can also configure scripts to execute your bun project. 

Configure your script in package.json as below:

{
  "name": "hello-server",
  "module": "index.ts",
  "type": "module",
  "scripts": {
    "start": "bun run index.ts"
  },
  "devDependencies": {
    "bun-types": "latest"
  },
  "peerDependencies": {
    "typescript": "^5.0.0"
  },
  "dependencies": {
    "figlet": "^1.7.0"
  }
}
and then execute your project using below command:

bun run start

Conclusion:

In this blog post, we learned how to create a simple node js like server using bun.sh. Whether you're a seasoned developer looking for a more streamlined experience or a beginner interested in exploring the world of server-side JavaScript, Bun.sh is worth a try. Give it a spin and discover the exciting possibilities it opens up for your JavaScript projects.

Post a Comment

0Comments

Please Select Embedded Mode To show the Comment System.*

Cookies Consent

This website uses cookies to offer you a better Browsing Experience. By using our website, You agree to the use of Cookies

Privacy Policy