← Back to list

Bun vs Node.js: Feature comparison between bun and Node.js

Compare both JavaScript runtime based on features.

Rajdeep Singh in FrontEnd Web · 2023-10-10 13:27 · 64 claps · 7.3 min read
#buns #bun-runtime #bun-feature #bun-vs-nodejs #nodejs
Open on Medium ↗
Wiki topics: 🌐 · Web Development

Bun vs Node.js: Feature comparison between Bun and Node.js

Bun vs Node.js: Feature comparison between Bun and Node.js

Bun vs Node.js

Bun vs Node.js: Feature comparison between Bun and Node.js

Compare bun and nodejs runtime based on features.

Bun is a new JavaScript runtime similar to node and deno. Bun is now stable and production-ready.

Before starting a comparison, first, we understand the history of Node.js and Bun. Then, we create a comparison.

Node.js

Node.js is an asynchronous event-driven open-source JavaScript runtime written with C++. Node.js' first release was launched on 27 May 2009 by Ryan Dahl, OpenJS Foundation, Bryan Cantrill.

Bun

Bun is an open-source JavaScript runtime, Package manager, bundler, and test runner, and it is written in Zig. Bun's first release was on 8 September 2023 by Jarred Sumner.

What issue does Bun solve?

Bun removed all the complicated configurations and surpassed Node.js regarding speed and ease.

You do not need to install Typescript, reactjs, a package manager(pnpm and yarn), a testing library, Transpilers, bundlers, etc, all built in Bun.

In simple words, the bun is an alternative or replacement for Node.js.

Comparison

We compare the Bun and nodejs-based features and describe what problem Bun solves or handles better than Node.js.

Steps

  1. Node.js Setup vs Bun Setup
  2. TypeScript and JSX support
  3. ESM & Common JS compatibility
  4. Web APIs
  5. Hot reloading
  6. Bun APIs
  7. Bun is a Package Manager
  8. Test runner
  9. Bundler
  10. Plugins

Node.js Setup vs Bun Setup

Setup in both nodejs and Bun is similar; Both provide an init command to create a setup.

npm init
# or
Bun init

The NPM command is a part of the node. The similar bunx command is part of the Bun.

Bun Init

$ bun init
bun init helps you get started with a minimal project and tries to guess sensible defaults. Press ^C anytime to quit

package name (bunInit): 
entry point (index.ts): 

Done! A package.json file was saved in the current directory.
 + index.ts
 + .gitignore
 + tsconfig.json (for editor auto-complete)
 + README.md

To get started, run:
  bun run index.ts

Bun init create node_modules, bun.lockb, tsconfig.json, package.json, and adding bun-types package.

├── bun.lockb
├── index.ts
├── node_modules
│   └── bun-types
│       ├── package.json
│       ├── README.md
│       ├── tsconfig.json
│       └── types.d.ts
├── package.json
├── README.md
└── tsconfig.json

2 directories, 9 files

Npm Init

$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help init` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (nodeinit) 
version: (1.0.0) 
description: 
entry point: (index.js) 
test command: 
git repository: 
keywords: 
author: 
license: (ISC) 
About to write to /home/officialrajdeepsingh/medium/bun-tutorial/nodeInit/package.json:

{
  "name": "nodeinit",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

Is this OK? (yes) 

The NPM init command creates apackage.json file.

TypeScript and JSX support

To enable Typescript and JSX in Node.js, you must install Typescript, ts-node, reactjs, and react-dom. On the other hand, in the Bun, everything pre-config compiles your project.

In Node

# Install the packages
npm install typescript ts-node react react-dom 

# run your file
npm run index.ts 

In Bun

bun index.ts
bun index.jsx
bun index.tsx

But running the reactjs application, first install the Reactjs with the following command.

bun install react 

# then run
bun index.jsx
bun index.tsx 

ESM & Common JS compatibility

Bun supports both ESM and Commons modules without writing additional configuration.

To understand how the ESM & Common JS module work with bun, check out the following example.

Common JS

function Add(a:number,b:number) {
    console.log("result:", a+b)
    return a+b
}

function Divider(a:number,b:number) {
    console.log("result:", a/b)
    return a/b
}

// export the module
module.exports = {
    Add,
    Divider
};

ESM

export function Subtract(a:number,b:number) {
    console.log("result:", a-b)
    return a-b
}

export function Multiple(a:number,b:number) {
    console.log("result:", a*b)
    return a*b
}

Now, you can use ESM and Common JS modules together in one file.

const { Add, Divider} = require("./commonjs")
import {Subtract, Multiple}  from "./esm"

Add(2,3)
Subtract(2,3)
Divider(2,3)
Multiple(2,3)

In Nodejs, you can not use Both ESM and Common JS modules in one file. If you use the ESM module, you need to configure add "type": "module" in your package.json. Nodejs has other file extensions, like .js, .cjs and .mjs.

Learn more about nodejs difference file extensions(.js, .cjs and .mjs), then check out this article written by Nipu Chakraborty.

Web APIs

Bun comes with in-built web APIs similar to Node.js. Many APIs are currently provided; most of the API is stable and production-ready.

For example, console, alert, confirm, prompt, JSON, fetch, response, etc. Check out the complete list of Bun APIs on docs.

// Fetch Bun web API: https://developer.mozilla.org/en-US/docs/Web/API/fetch

const response = await fetch("https://dummyjson.com/posts?limit=10");

// Response Bun web API: https://developer.mozilla.org/en-US/docs/Web/API/Response

const result = await response.json();

// Console Bun web API: https://developer.mozilla.org/en-US/docs/Web/API/console

console.log(result.posts)

In nodejs, you depend on the third-party package for a few APIs. A good example is node-fetch, ws (Web Socket), etc.

import fetch from 'node-fetch';

const response = await fetch('https://dummyjson.com/posts?limit=10');
const result = await response.json();

console.log(result);

Hot reloading

Bun improves your development experience; you can use the--hot option to enable hot reloading.

Hot reloading is to reload your application in the browser when you change or edit your project file.

# Reload module
bun --hot server.ts

# Reload Nextjs
bun --hot run dev

Bun APIs

Web APIs and Bun APIs are both different. Bun APIs are specified and used inside Bun. It is a specific design and was built for the Bun.

You can not use Bun APIs with another runtime engine like Deno and nodejs.

Bun provides many built-in APIs such as Bun.serve, Bun.build, Bun.file, Bun.write, bun:sqlite, etc.

Check out the complete list of bun APIs on docs.

Run your local development server with bun API using the following code.

const index = Bun.file("./index.html");

Bun.serve({
    fetch(req: Request) {
        return new Response(index)
    },
    port: 1337
})

console.log("Server is running on port 1337!")

You can also create a local development server using web API in the node. But nodejs has no special API like bun API, and you can not use bun API with nodejs.

const http = require("http");

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader("Content-Type", "text/plain");
  res.end("Hello, world!");
});

server.listen(3000, () => {
  console.log("Server listening on port 3000");
});

Bun is a Package Manager

Bun has a built-in package manager to speed up your development workflow.

You do not need to use or install additional software with Bun. Bun package manager gives you a similar experience to NPM, yarn, and pnpm.

bun install

bun install command is used to install everything mentioned in the package.json file. It installs the nodejs package from npmjs.org. It is a similar command to the NPM install command.

bun install
# Learn more about bun install command
bun install --help

In node or NPM, we used the NPM install command.

npm install
# Learn more about npm install command
npm install --help

bun add <package> [ — dev| — production| — peer]

bun add command is used to install the nodejs package from npmjs.org. It is a similar command to the NPM install command.

You can even install packages globally in your system, similar to NPM.

bun add <package-name>
# or install global package with bun
bun add -g <package-name>
#  or install or adding package as a dev Dependencies
bun add -d <package-name>
# Learn more about bun add command
bun add --help

In the node, we used the NPM install command to install any package from the NPM repository.

npm install <package-name>
# or install global package with bun
npm install -g <package-name>
#  or install package as a devDependencies
npm install -d <package-name>
# Learn more about bun install command
npm install --help

bun remove <package>

The Bun remove command is useful when we remove any package from our project and remove the package from the global installation package.

We can remove single or multiple packages.

bun remove <package-name>
# or remove multiple package
bun remove <package-name> <package-name>
# or remove the global package
bun remove -g <package-name> <package-name>

We used the NPM uninstall command in the node to remove the package.

npm remove <package-name>
# or remove multiple package
npm remove <package-name> <package-name>
# or remove the global package
npm remove -g <package-name> <package-name>

bun update <package>

Bun update command, update your outdated or old package in your project.

The Bun update command exit works like the NPM update command.

bun update
# update the global package
bun update -g
# update the single package
bun update <package-name>
# update the muiltiple package
bun update <package-name> <package-name>

In nodejs, we also use the NPM update command to update the old or outdated package.

npm update
# update the global package
npm update -g
# update the single package
npm update <package-name>
# update the muiltiple package
npm update <package-name> <package-name>

Test runner

Bun comes with an inbuilt test runner. You do not need to install any third-party package for testing.

Bun test works similarly to the nodejs Jest library. Check out all feature lists provided by Bun.

import { expect, test } from "bun:test";

test("2 + 2", () => {
  expect(2 + 2).toBe(4);
});

In node, we first install the jest as dev dependencies in your project, then configure or add "test”:”jest” script in the package json file. Lastly, create a test file with the following syntax anyfilename.test.js.

// sum.test.js
import { expect, test } from "jest";

test("2 + 2", () => {
  expect(2 + 2).toBe(4);
});

Bundler

Bun supports bundling your JavaScript and typescript projects without additional configuration and installing third-party packages.

bun build ./index.tsx --outdir ./build

In your nodejs, you need a web pack, Babel, Turbo pack, Turbo repo, etc, installed according to your project requirement.

Plugins

Bun provides a universal plugin API that can provide additional functionality.

Bun Plugin API works during runtime and *bundler*. You can quickly build your plugin and use a third plugin.

Use Third-party plugin

// use third party plugin 

import { plugin } from "bun";
import fooPlugin from "bun-plugin-foo";

plugin(
  fooPlugin({
    // configuration
  }),
);

Use or build our own Custom Plugin


import { plugin } from "bun";

plugin({
  name: "my-custom-plugin",
  async setup(build) {

   console.log(build)

  },
});

Conclusion

Bun has recently got a stable release, and It reduces configuration for your project.

In nodejs, use any modern tool like Typescript, bundler, packager manager, etc.

You depend on a third-party package, and every time, in some packages, you need to write configuration again and again.

You can share and follow me on Twitter and LinkedIn. I write tons of articles related to frontend development and Linux.

If you are interested in those topics, follow me on Medium, officialrajdeepsingh.dev, join the Linux and frontend web publication, and sign up for my free newsletter.


메타데이터
post_id
3373ba095a3f
slug
bun-vs-node-js-feature-comparison-between-bun-and-node-js-3373ba095a3f
url
https://medium.com/frontendweb/bun-vs-node-js-feature-comparison-between-bun-and-node-js-3373ba095a3f
canonical_url
https://medium.com/frontendweb/bun-vs-node-js-feature-comparison-between-bun-and-node-js-3373ba095a3f
author_url
https://medium.com/@officialrajdeepsingh
status
ok
fetched_at
2026-07-29 20:50:29