Learning Bun Basics: A Faster JavaScript Runtime

2024-02-16 · 2 min read

If you're used to Node.js plus a pile of extra tools — ts-node, nodemon, Jest, npm — Bun offers a different pitch: one runtime that ships with most of that built in. Built on JavaScriptCore, Bun executes code noticeably faster than Node.js and understands TypeScript out of the box, no extra compilation step required.

Why Bun is worth trying

The case for Bun comes down to speed and simplicity. It runs .ts files directly, no separate compiler needed, and ships its own package manager that installs and updates dependencies noticeably faster than npm. For small-to-medium projects, that means fewer tools to wire together and less time spent waiting.

From install to first project

Installation is a single OS-specific command from Bun's official docs, run in your terminal. Confirm it worked with:

bun --version

From there, bun init scaffolds a basic project structure. Running a file is just as direct:

bun run index.ts

Bun understands TypeScript and JSX natively — add React via bun add react along with its type definitions if you want JSX support. For anything more specific, a regular tsconfig.json still works as expected.

Built-in features you'll actually use

A few things that usually need extra libraries in Node.js come bundled with Bun itself:

  • Environment variables — drop them in a .env file and read them via process.env, same as always.
NextThe Hidden Danger of Copying Commands from the Web
  • Watch mode — add the --watch flag when running a file to auto-reload on every code change, handy during development.
  • Package management — bun add, bun remove, and bun update handle dependencies without installing anything extra.
  • Testing — bun test runs a built-in test runner that's Jest-compatible, so migrating existing tests is usually painless.
  • Production builds — bun build input.ts --outdir dist bundles the project, with flags like --minify to shrink output or --compile to produce a standalone executable.
  • All of this works with a single Bun binary — no extra dev dependencies required.

    Taken together, Bun isn't just a faster Node.js — it's a leaner runtime for everyday work, since tools that are normally separate come already fused into one.