DEV Community

Manthan Ankolekar
Manthan Ankolekar

Posted on

2

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/.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay