Setting up Bun.js Projects
0 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 lockfileindex.ts
- Your entry TypeScript filepackage.json
- Project metadatatsconfig.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!

Share:
Comments
Waiting for your comments