Bun - first impressions
After trying out Deno, the next thing on the list to look at is Bun. Key features:
- Batteries included - like Deno
- Drop-in replacement for node.js
- Much faster then both node.js and Deno due to JavaScriptCore
So lets play!
Installation
- Very easy - one curl-bash command
- Docker image also available
- Doesnt seem to be any OS level package (deb, rpm, etc) yet
Quickstart
Quickstart instructions work perfectly, bun init
created a very minimal set of files:
├── bun.lockb
├── .gitignore
├── index.test.ts
├── index.ts
├── node_modules
├── package.json
├── README.md
└── tsconfig.json
Unit testing
bun test
works out of the box if you create a .test.ts
file and is very quick
Linting/Formatting
biome seems to be the way to cleanup code on bun. Its quick and easy to setup:
bun add -d @biomejs/biome
bun x @biomejs/biome check --apply .
Native code (node-gyp)
Seems to work! I was able to install the Confluent Kafka library, although I didnt try using it:
bun add @confluentinc/kafka-javascript
bun pm trust @confluentinc/kafka-javascript
Node project migration
So far bun is ticking all the right boxes so lets try migrating a node.js project to bun in a git branch.
Conversion
Cleanup node remnants:
# eg:
rm .eslintrc.cjs package-lock.json tsconfig.* vitest* node_modules
Convert to bun project:
bun init
Cleanup stray bun settings (this project is part of a monorepo):
rm .gitignore
Reinstall all dependencies from package.json
- Could have probably re-used the existing node_modules
directory but wanted to prove can still resolve modules:
bun install
Thats it!
Re-run tests
This project has a bunch of vitest tests - most of them passed but there were some errors running integration tests, probably because of using node to run commands on the host. I can work with this :)
bun test
Result: Mostly working
Run the project
Moment of truth:
# eg
bun run src/app.ts
Result: Works first time!!! This is a resonably complex app that interfaces remote APIs through a node library and other binary network services. My face: 😳
Docker packaging
- Change
FROM
to reference a Bun tag - Change the
RUN node ...
toRUN bun ...
- Make sure to run
.ts
files directly - there is no compiled JavaScript to deal with - Thats it!
Result: Mostly working. I need to upgrade my shared
dependency to also use bun and then enable it for linking with bun link
, hopefully this resolves #5045
Conclusion
Highly promising technology with similar enjoyment level to Deno. Im converting the rest of my project to use Bun!