Run JS with Bun.js
0 119
Introduction
Running JavaScript code with Bun.js is fast, efficient, and refreshingly simple. Unlike traditional JavaScript runtimes that require multiple tools for setup, Bun empowers developers to execute JavaScript (and TypeScript) files directly using its built-in command-line interface. In this guide, we’ll walk you through how to run JS with Bun.js and why it’s such a game-changer for developers.
Why Use Bun.js to Run JavaScript?
Bun isn’t just another Node.js alternative. It’s a high-speed, all-in-one JavaScript runtime built with performance in mind. Here’s why developers prefer Bun for running JS code:
- Faster execution time (written in Zig)
- Built-in TypeScript and JSX support
- No external compilers needed
- Fewer dependencies, less setup
Installing Bun.js
If you haven’t installed Bun yet, start by running the following command in your terminal:
curl -fsSL https://bun.sh/install | bash
Once installed, restart your terminal and check if Bun is available:
bun --version
Writing a Simple JavaScript File
Let’s create a basic JavaScript file called hello.js
:
// hello.js
console.log("Hello from Bun.js!");
This is a standard JS script. No special syntax or configuration needed.
Running the JS File with Bun
To run your script, use the bun run
command:
bun run hello.js
You’ll instantly see the output:
Hello from Bun.js!
No Babel, no Webpack, no waiting — just fast execution.
Running JavaScript Without 'run'
Bun can also run JavaScript files directly by just calling:
bun hello.js
This shortcut behaves the same as bun run
and is perfect for quick scripts or one-off tasks.
Running TypeScript or JSX Files
Bun supports TypeScript and JSX out of the box. No need for separate compilers. Create a file like app.ts
:
// app.ts
const greet = (name: string) => `Hello, ${name}`;
console.log(greet("Bun Developer"));
Then run it directly:
bun app.ts
This makes Bun a powerful tool for TypeScript developers who want to avoid complex setups.
Using ES Modules and Imports
Bun fully supports ES modules, so you can use import
syntax without any config:
// utils.js
export function add(a, b) {
return a + b;
}
// index.js
import { add } from "./utils.js";
console.log("Result:", add(2, 5));
Then run it with:
bun index.js
Command Line Arguments with Bun
You can access command-line arguments the same way as in Node.js:
// args.js
console.log("Arguments:", process.argv);
bun args.js first second
This prints all the arguments passed, starting from the Bun binary path.
Bun vs Node: Execution Speed
One of the biggest reasons developers prefer Bun is its execution speed. Benchmarks show that Bun executes scripts significantly faster than Node.js, especially when dealing with file I/O and module resolution.
Conclusion
Running JS with Bun.js simplifies your workflow and speeds up development. With its built-in TypeScript support, instant execution, and powerful CLI, Bun is quickly becoming the go-to runtime for modern JavaScript developers. Whether you're writing quick utilities or full-scale applications, Bun can run your code faster and more efficiently than traditional setups.
If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!

Share:
Comments
Waiting for your comments