Getting started with Bun: A beginners guide
Tags: bunjs, webdev, javascript, typescript
Published: 2024-05-19 11:41:14

## What is Bun?
Bun is a new JavaScript runtime and package manager that focuses on speed and efficiency. It leverages the JavaScriptCore engine (the same engine used by Safari) to deliver high performance. Here are some standout features of Bun:

1. Performance: Bun is designed to be one of the fastest JavaScript runtimes available, with various optimizations that speed up development tasks.
2. Compatibility: Bun is highly compatible with Node.js and npm packages, making it easy to migrate existing projects or start new ones.
3. Built-in Tools: Bun includes a built-in bundler, transpiler, and task runner, reducing the need for multiple development tools.
4. TypeScript Support: Bun offers first-class support for TypeScript, simplifying the development process for TypeScript developers.

## Why Choose Bun?
**Speed**: Bun's primary selling point is its speed. By leveraging the JavaScriptCore engine, Bun can execute JavaScript code more quickly than many other runtimes.

**Developer Experience:** Bun aims to streamline the development process with built-in tools that reduce setup time and complexity. This means less time configuring and more time coding.

**Compatibility:** Bun's compatibility with Node.js and npm packages ensures that developers can easily transition to using Bun without abandoning their existing workflows and libraries.

## Setting Up Bun
### Installation
Before you can start using Bun, you'll need to install it on your system. Here’s how to do it using **npm**:

``` terminal
npm install -g bun
```
and thats it. Bun is now successfully installed in your system.

## Building Your First Project with Bun

### Initializing first Bun project:
``` terminal
bun init
```
This command will initialize a simple bun project. A basic bun project looks like this:

```
my-bun-project/
├── node_modules
├── index.ts
├── .gitignore
├── README.md
├── bun.lockb
└── package.json
```

### Running Bun project:
``` terminal
bun run index.ts
```
You can run your project using bun run command. Additionally you can append scripts object with this command in package.json.
```
"scripts": {
    "dev": "bun run index.ts"
}
```

Now if you run this command in terminal
```
bun dev
```
then you should see this in your terminal
``` output
Hello via Bun!
```

Voila you just created and executed your first bun project. Now lets see how we can install packages.

## Installing packages using Bun

In order to install packages using bun you can run this command
```
bun add <package-name> # alternative of npm i 
```

If you want to run npx scripts using Bun you can run this command:
```
bun x <script-name>
```  
If you want to install all dependencies that are present in package.json like `npm install`, you can use this command to do this in Bun.
```
bun install
```
If you want to update all dependencies that are present in package.json like `npm update`, you can use this command to do this in Bun.
```
bun update
```

## Using installed Packages
You can easily use installed packages by importing them in your file and use them as usual:

```
import express from "express";
const app = express();
```

These are all the essential things for a begginer to start using `Bun`

## Conclusion

Bun is a powerful and efficient JavaScript runtime and package manager, known for its speed and developer-friendly features. It offers high compatibility with Node.js and npm packages, built-in tools like a bundler, transpiler, and task runner, and out-of-the-box TypeScript support. With simple installation and intuitive commands, Bun streamlines the development process, allowing developers to focus on coding rather than setup. Whether starting a new project or migrating an existing one, Bun provides a robust environment that enhances productivity and performance. keep growing and Happy coding!.

### Additional Resources

- Official Documentation: Bun [Documentation](https://bun.sh/docs)
- GitHub Repository: Bun [GitHub](https://github.com/oven-sh/bun)
- Community Links: Bun [Community](https://discord.com/invite/bun-876711213126520882)
- Further Reading: Explore more articles and tutorials on Bun and related technologies.