Bun.js Env Setup
0 886
๐ฑ Introduction to Environment Setup in Bun.js
Setting up your environment correctly is crucial when working with any modern JavaScript runtime, and Bun.js is no exception. Bun offers fast performance, a powerful runtime, and developer-friendly tooling.
This tutorial will walk you through how to set up a clean development environment for building applications using Bun.js.
๐ฆ Prerequisites
Before you get started, ensure you have the following:
- Operating System: macOS, Linux (Windows support is still evolving)
- Terminal or command-line access
- Basic understanding of JavaScript
โ๏ธ Installing Bun.js
Bun provides a very simple installation command. You can install it via curl:
curl -fsSL https://bun.sh/install | bash
This script will install the latest version of Bun and configure your shell automatically.
Restart your terminal session to apply changes.
๐ Verifying Installation
To check if Bun was installed successfully, run the following command:
bun --version
If everything worked, it should output the installed version of Bun.
๐ Creating a Project
You can create a new Bun project using:
bun init
This will prompt you with a few questions and create a basic project with the right structure. It will include:
index.tsorindex.jspackage.jsonbun.lockb
๐ง Managing Environment Variables
Like Node.js, Bun also supports environment variables. To manage sensitive or configuration-based values, use a .env file in the root of your project.
# .env
API_KEY=abc123
ENV=development
You can access these variables in your code using process.env:
console.log("API Key:", process.env.API_KEY);
๐ช Auto Reload with Watch Mode
Bun doesnโt support hot reloading out-of-the-box like some tools, but you can enable auto-restart by running:
bun --watch index.ts
This is useful when making rapid code changes and testing behavior instantly.
๐ ๏ธ Setting Up Scripts in package.json
To make your workflow easier, you can define scripts in package.json:
"scripts": {
"start": "bun index.ts",
"dev": "bun --watch index.ts"
}
Now you can run bun run dev to automatically restart your app on file changes.
๐ Installing Dependencies
Bun comes with its own ultra-fast package manager. You can install any package like this:
bun add axios
It will automatically update your package.json and generate a bun.lockb file to track dependencies.
๐ Final Thoughts
Setting up your environment with Bun.js is fast and smooth, thanks to its all-in-one ecosystem. From dependency management to file watching and environment variables, Bun provides everything you need to get started with full-stack JavaScript development.
Ready to build blazing fast apps? Start with Bun and experience the future of JavaScript tooling!
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