DEV Community

Cover image for Getting started with Bun: A beginners guide
Talha Ali
Talha Ali

Posted on

Getting started with Bun: A beginners guide

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:

npm install -g bun
Enter fullscreen mode Exit fullscreen mode

and thats it. Bun is now successfully installed in your system.

Building Your First Project with Bun

Initializing first Bun project:

bun init
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Running Bun project:

bun run index.ts
Enter fullscreen mode Exit fullscreen mode

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"
}
Enter fullscreen mode Exit fullscreen mode

Now if you run this command in terminal

bun dev
Enter fullscreen mode Exit fullscreen mode

then you should see this in your terminal

Hello via Bun!
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

If you want to run npx scripts using Bun you can run this command:

bun x <script-name>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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();
Enter fullscreen mode Exit fullscreen mode

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
  • GitHub Repository: Bun GitHub
  • Community Links: Bun Community
  • Further Reading: Explore more articles and tutorials on Bun and related technologies.

Top comments (0)