Quick Start Guide with Bun.js

Quick Start Guide with Bun.js

ยท

2 min read

To quick start with Bun.js, follow these steps:

  1. Install Bun. You can install Bun from the website or with your package manager.

  2. Initialize a new project. Run bun init in an empty directory.

  3. Create a server. Create a file called index.ts and add the following code:

TypeScript

import { serve } from "bun";

const server = serve({
  fetch() {
    return new Response("Hello, world!");
  },
  port: 3000,
});

console.log("our server is running at " + server.port )
  1. Start the server. Run bun run in the project directory.

  2. Open a web browser and navigate tohttp://localhost:3000. You should see the message "Hello, world!"

You can now modify the index.ts file to create your own server application. Bun also includes a package manager, bundler, and test runner, so you can use it to build and run complete JavaScript applications.

Here is an example of how to install a package with Bun:

bun install cowsay

This will install the figlet package and its type declarations. You can then use the figlet package in your code:

TypeScript

import cowsay from "cowsay";

const cow = cowsay.say({
  text: "Hello, world!",
  cow: "default",
});

console.log(cow);

This will print the following output to the console:

     (__)
       (oo)
   /------\/
 * / |    ||
    ~~   ~~

You can learn more about Bun on the official website: https://bun.sh/.

Buy Me A Coffee

Did you find this article valuable?

Support Revive Coding by becoming a sponsor. Any amount is appreciated!

ย