Setting up Bun.js Projects
×

Setting up Bun.js Projects

138

๐Ÿš€ Setting up Bun.js Projects

If you're looking for a lightning-fast, all-in-one JavaScript runtime, bundler, transpiler, and package manager โ€” Bun.js is what you need. It's built from the ground up in Zig and optimized for performance, offering a modern alternative to Node.js and npm. In this guide, weโ€™ll walk through how to set up a fresh Bun.js project from scratch.

๐Ÿ”ง Prerequisites for Bun

Before you get started, make sure you have the following:

  • macOS, Linux, or WSL2 (Windows)
  • curl installed on your system
  • Basic command line knowledge

Ready? Letโ€™s go!

๐Ÿ“ฆ Installing Bun.js

The easiest way to install Bun is via the terminal using curl:

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

After installation, restart your terminal or run the following command to add Bun to your shell environment:

source ~/.bashrc    # or ~/.zshrc depending on your shell

๐Ÿ“ Creating a New Bun Project

Once Bun is installed, you can scaffold a new project instantly with:

bun init

This will prompt you to enter details like:

  • Project name
  • Entry point (default: index.ts)
  • Package manager (Bun is selected automatically)

Bun will then generate a project structure that includes:

  • bun.lockb - Bunโ€™s lockfile
  • index.ts - Your entry TypeScript file
  • package.json - Project metadata
  • tsconfig.json - TypeScript config

โš™๏ธ Running Your Bun Project

To run your app, simply use:

bun index.ts

Thatโ€™s it! Bun executes your TypeScript or JavaScript files directly โ€” no need for compilation or transpilation steps.

๐Ÿงช Adding Dependencies

Bun includes its own ultra-fast package manager, which means no more waiting on slow installs. Install packages like this:

bun add axios

And for dev dependencies:

bun add -d typescript eslint

No need for separate tools โ€” Bun handles it all.

๐Ÿ“œ Example index.ts File

Hereโ€™s a simple example using Bun to create a basic HTTP server:

const server = Bun.serve({
  port: 3000,
  fetch(req) {
    return new Response("Hello from Bun.js! ๐ŸŽ‰");
  }
});

console.log("Server running at http://localhost:3000");

Run it using:

bun index.ts

๐Ÿ“‚ Optional: Creating Directory Structure

You can structure your Bun.js project however you'd like. Hereโ€™s a common setup:

my-bun-app/
โ”œโ”€โ”€ src/
โ”‚   โ””โ”€โ”€ index.ts
โ”œโ”€โ”€ public/
โ”‚   โ””โ”€โ”€ index.html
โ”œโ”€โ”€ bun.lockb
โ”œโ”€โ”€ package.json
โ”œโ”€โ”€ tsconfig.json

โšก Bun Features You Should Explore Next

  • Built-in test runner using bun test
  • Fast transpilation with built-in esbuild-like compiler
  • Plugin system for handling custom file types
  • Hot reloading for development efficiency

๐ŸŽฏ Final Thoughts

Setting up Bun.js Projects is as simple as it is powerful. With a single command, you get everything you need to start building fast and efficient applications. Bunโ€™s performance, combined with its built-in tooling, makes it a solid choice for modern JavaScript and TypeScript development. Give it a spin, and you might not want to go back!



If youโ€™re passionate about building a successful blogging website, check out this helpful guide at Coding Tag โ€“ How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat