Bun.js cheatsheet

manthanank

Manthan Ankolekar

Posted on September 11, 2023

Bun.js cheatsheet

Install Bun:

curl -fsSL https://bun.sh/install | bash
Enter fullscreen mode Exit fullscreen mode

Run a JavaScript/TypeScript file:

bun index.js
Enter fullscreen mode Exit fullscreen mode

Run a package.json script:

bun run dev
Enter fullscreen mode Exit fullscreen mode

Test your code:

bun test
Enter fullscreen mode Exit fullscreen mode

Install a package:

bun install express
Enter fullscreen mode Exit fullscreen mode

Execute a command:

bunx cowsay 'Hello, world!'
Enter fullscreen mode Exit fullscreen mode

Get the current Bun version:

bun version
Enter fullscreen mode Exit fullscreen mode

Hash a password:

bun hash password
Enter fullscreen mode Exit fullscreen mode

Escape an HTML string:

bun escape "<script>alert('Hello, world!')</script>"
Enter fullscreen mode Exit fullscreen mode

Check if two objects are deeply equal:

bun deep-equal {a: 1, b: 2} {a: 1, b: 2}
Enter fullscreen mode Exit fullscreen mode

Detect when code is executed with Bun:

bun is-bun
Enter fullscreen mode Exit fullscreen mode

Get the directory of the current file:

bun cwd
Enter fullscreen mode Exit fullscreen mode

Get the file name of the current file:

bun filename
Enter fullscreen mode Exit fullscreen mode

Convert a file URL to an absolute path:

bun url-to-path file:///path/to/file.js
Enter fullscreen mode Exit fullscreen mode

Compress and decompress data with gzip:

bun gzip data
bun gunzip data
Enter fullscreen mode Exit fullscreen mode

Convert an absolute path to a file URL:

bun path-to-url /path/to/file
Enter fullscreen mode Exit fullscreen mode

Sleep for a fixed number of milliseconds:

bun sleep 1000
Enter fullscreen mode Exit fullscreen mode

Compress and decompress data with DEFLATE:

bun deflate data
bun inflate data
Enter fullscreen mode Exit fullscreen mode

For more information, please refer to the Bun documentation: https://bun.sh/.

💖 💪 🙅 🚩
manthanank
Manthan Ankolekar

Posted on September 11, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related