Move Your Node.js App to Bun: A Beginner's Guide

Node.js has been a popular choice for beginners and startups in web development. But since September 2023, Bun has been gaining attention for its lightning-fast performance.

What's Node.js?

Node.js lets you run JavaScript outside of web browsers, making server-side programming easy. It's fast and has a wide range of tools (like NPM packages) for developers.

What's Bun?

Bun is a toolkit for JavaScript and TypeScript apps, boasting incredible speed. It's built using the JavaScript Core Engine, making it faster than Node.js. Plus, Bun serves as a package manager, bundler, test runner, and runtime environment.

Why Switch to Bun?

  1. Adaptability: Bun works seamlessly with existing Node.js apps and packages.

  2. Bun APIs: Bun offers faster and easier alternatives to Node.js APIs.

  3. TypeScript Support: Setting up TypeScript is simpler with Bun.

  4. Path Mapping: Bun's unique approach makes imports cleaner.

  5. Hot Reloading: Bun comes with built-in hot reloading for faster development.

  6. Package Management: Bun installs dependencies much faster than other package managers.

  7. Test Case Execution: Bun includes Jest for easy testing.

  8. Application Bundling: Bun's bundler is significantly faster than others like Webpack.

Sounds convincing doesn’t it? Let’s migrate an app from Node.js to Bun

First things first, you’ll want to install Bun on your PC. To do so, run the command:

curl -fsSL https://bun.sh/install | bash

If you’ve successfully installed Bun, you should get the output:

######################################################################## 100.0%
bun was installed successfully to ~/.bun/bin/bun
Run 'bun --help' to get started

Next, launch a terminal in your project directory and install Bun using the command:

bun install

This command installs the BUN, but it gives the below error.

bun install v1.0.14 (d8be3e51)
error: Please upgrade package-lock.json to lockfileVersion 3

This error occurs when there’s a mismatch between the version specified in your package-lock.json file (lockfileVersion) and the expected version.

So the given command will upgrade the lockfile to version 3 without altering the existing dependencies.

Next, go ahead and remove all TypeScript related libraries from your package.json as Bun comes with pre-built support for TypeScript. This can be done using the command:

bun remove ts-node
bun remove typescript
bun remove @types/node

You should see the output:

bun remove v1.0.14 (d8be3e51)
 - ts-node
 1 package removed [204.00ms]
bun remove v1.0.14 (d8be3e51)
 - typescript
 1 package removed [23.00ms]
bun remove v1.0.14 (d8be3e51)
 - @types/node
 1 package removed [23.00ms]

Next, go ahead and add the types offered by Bun itself for better TypeScript performance, using the command:

bun add bun-types

This should display the output:

bun add v1.0.14 (d8be3e51)
 installed bun-types@1.0.14
[620.00ms] done

Next, open your tsconfig.json and ensure that you’re using the bun-types.

{
  "compilerOptions": {
    "lib": ["ESNext"],
    "module": "esnext",
    "target": "esnext",
    // "bun-types" is the important part
    "types": ["bun-types"]
  }
}

Next, alter your NPM Start command to use Bun, as shown below:

"start": "bun app.tsx"

And, that’s it! You’ve successfully migrated your app from Node.js to Bun. To launch your app with Bun, use the command:

bun start

This will launch your app you normally do!

Conclusion

Bun is taking the world by storm with its immense features and performance. If you’re one of the developers that love building high performing apps with minimal effort, you’re better off working with Bun!

I hope you found this article helpful.

Did you find this article valuable?

Support Annamalai Palani by becoming a sponsor. Any amount is appreciated!