How to Use Bun Runtime for JavaScript: A Beginner’s Tutorial
Bun is a fast all‑in‑one runtime for JavaScript and TypeScript, designed to replace Node.js with better performance and built‑in tools like a bundler, test runner, and npm‑compatible package manager. In this tutorial, you’ll learn the essential commands to install Bun and start running, testing, and managing JavaScript projects.
Install Bun
Installing Bun is straightforward. Open your terminal and run:
- macOS / Linux:
curl -fsSL https://bun.sh/install | bash - Windows (WSL): Use the same command inside WSL2
- Alternatively, use
npm install -g bun(slower)
After installation, verify it with bun --version.
Run JavaScript Files
Bun executes scripts as fast as possible. Use:
bun run index.js– runs a single filebun run --watch index.js– auto‑restarts on file changes (ideal for development)- No need for a separate
node_modulessetup; Bun uses its own module resolution
Manage Dependencies
Bun can replace npm or yarn. Common commands:
bun init– creates a newpackage.jsonbun install– installs all dependencies frompackage.jsonbun add express– adds a package and saves it topackage.json
Run Tests with Bun
Bun includes a built‑in test runner compatible with Jest syntax. Write tests in any .test.js file, then run:
bun test– discovers and runs all test files- Example test:
describe('sum', () => { expect(1+1).toBe(2); });
Conclusion: Bun simplifies the JavaScript workflow by combining runtime, package manager, and test runner into one blazing‑fast tool. Experiment with it today to speed up your development.