How to use Bun
What is Bun?
Bun is a modern JavaScript runtime like Node.js or Deno, but designed to be faster and more efficient. It includes an all-in-one toolkit with a JavaScript/TypeScript runtime, bundler, transpiler, and package manager. Here's a quick guide on how to get started with Bun.
1. Install Bun
npm install -g bunor
powershell -c "irm bun.sh/install.ps1|iex"or (Mac)
curl -fsSL https://bun.sh/install | bashThis will download and install Bun. Once installed, you can verify it with:
bun --versionThe current version is 1.1.26
2. Creating a New Project
To create a new project using Bun, you can simply initialize it with the following command:
bun initThis will prompt you with options to create a basic project setup, similar to how npm or yarn works.
3. Running a Script
bun run index.js4. 4. Using Bun as a Package Manager
Bun acts as a package manager similar to npm or yarn, but faster. You can install packages using:
bun add expressBun also automatically generates a bun.lockb lockfile for dependency management.
5. Bundling Code
Bun has a built-in bundler, which can bundle your JavaScript or TypeScript files for deployment:
bun build index.tsThis will create an optimized bundle for your project.
6. Running a Development Server
You can use Bun to quickly spin up a development server for web apps:
bun devThis will serve your application and automatically reload changes as you update your code.
7. Using Bun with Frameworks
Bun supports frameworks like React, Vue, and others. For example, you can start a React project by running:
1 bun create react-app my-app2 cd my-app3 bun install4 bun dev
Conclusion
Bun offers a streamlined and fast environment for building JavaScript and TypeScript projects. With its integrated package manager, bundler, and dev server, Bun provides an all-in-one solution that enhances developer productivity.