Self-Contained TypeScript Programs Using Bun

Self-Contained TypeScript Programs Using Bun

Bun TypeScript auto-install demonstration

Bun’s auto-install feature

If you hate Python as much as me it’s probably because of dependencies.

Roughly 23-319% of the time, when I run a Python app, it doesn’t work because of dependencies, and I end up trying to figure out which of my 17 real and virtual Python environments are actually active.

Using uv for everything is way better, but since I’m kind of moving my entire programming world to TypeScript, I’m now using bun‘s auto-install feature instead. And it’s actually a bit better.

Different Approaches, Same Goal

Python’s uv and Bun both solve the “self-contained app” problem by putting the requirements inside the program itself, but they do it in different ways:

Python uses special magical comments to declare dependencies:

It works great, but it feels super hack-y to me.

It feels like we’re smuggling in a dependency payload to trick Python into actually working for once.

Bun’s Approach: Just import it like normal

I like bun‘s approach much better. It just writes the imports out like it’s not embarrassed by them!

Running the Script

The first time you run the script, bun automatically:

  1. Detects the missing packages
  2. Downloads and installs them
  3. Caches them for future runs
  4. Executes your script

No npm install, no package.json, no setup—just run it.

Example Output

Oh, and it’s nuclear fast.

My takeaway

This goes to a larger discussion around Python vs TypeScript, but I feel like this is another example where the latter is just a more natural, modern way of doing things.

TypeScript all the things.


Source link