DEV Community 👩‍💻👨‍💻

openHacking
openHacking

Posted on • Updated on

Install Bun for Windows: A Modern JavaScript Runtime Like Node or Deno

Introduction

Recently, a project called Bun in the front-end tool chain has become popular. Bun is a new JavaScript runtime with built-in bundler, transpiler, task runner and npm client.

Bun is a modern JavaScript runtime like Node or Deno. Unlike Nodejs, Bun extends jsCore, not V8. Bun natively implements hundreds of Node.js and Web APIs, including ~90% of Node-API functions (native modules), fs, path, Buffer and more.

The goal of Bun is to run most of the worlds JavaScript outside of browsers, bringing performance and complexity enhancements to your future infrastructure, as well as developer productivity through better, simpler tooling.

I plan to try installing Bun on Windows to experience it.

Environment Preparation

1.Install WSL

The official website provides a one-click installation script, but it can only run on Linux, macOS, and WSL. The Windows desktop environment cannot be installed, so if you want to install on Windows, you need to install the Linux subsystem, which is WSL.

For specific steps, please refer to Microsoft's official documentation Install Linux on Windows with WSL

It is recommended to use Windows Terminal to quickly open the WSL terminal: Microsoft Store: Windows Terminal

2.Make sure the BIOS virtual machine function is turned on

Enter the BIOS to set the virtualization technology Intel Virtual Technology, disabled by Disable to Enable enabled.

3.Enable Hyper-v

PowerShell or CMD.exe run in administrator mode

bcdedit /set hypervisorlaunchtype auto
Enter fullscreen mode Exit fullscreen mode

For the details of virtual machine and Hyper-v settings, you can also refer to this blog post: "VMware Workstation Unrecoverable Error: (vcpu-1)" Troubleshooting for Vmware Workstation16

Install Bun

1.Make sure that the WSL system (mine is Ubuntu) has unzip installed, otherwise an error will be reported when installing Bun

error: unzip is required to install Bun (see: https://github.com/Jarred-Sumner/bun#unzip-is-required
Enter fullscreen mode Exit fullscreen mode

Use Terminal to open WSL, Ubuntu system install unzip with the following command

sudo apt-get install unzip
Enter fullscreen mode Exit fullscreen mode

2.Enter under WSL

 curl https://bun.sh/install | bash
Enter fullscreen mode Exit fullscreen mode

The following prompt appears, the installation is successful

Bun was installed successfully to /root/.bun/bin/bun

Manually add the directory to your $HOME/.bashrc (or similar)

   BUN_INSTALL="/root/.bun"
   PATH="$BUN_INSTALL/bin:$PATH"
Enter fullscreen mode Exit fullscreen mode

3.Manually add environment variables as prompted

Open the .bashrc file with vim

vim /root/.bashrc
Enter fullscreen mode Exit fullscreen mode

Add the following environment variable settings to the end of the .bashrc file and save it

BUN_INSTALL="/root/.bun"
PATH="$BUN_INSTALL/bin:$PATH"
Enter fullscreen mode Exit fullscreen mode

Exit and re-enter WSL to check if the installation is successful

bun -h
Enter fullscreen mode Exit fullscreen mode

4.Test Module

Use the WSL extension for vscode to manage projects in a WSL system

Official detailed tutorial Get started using VSCode with WSL

We create a new http.js file and write the code

// http.js
export default {
  port: 3000,
  fetch(request) {
    return new Response("Welcome to Bun!");
  },
};
Enter fullscreen mode Exit fullscreen mode

start http service

bun run http.js
Enter fullscreen mode Exit fullscreen mode

Visit http://localhost:3000 to see the Welcome to Bun! message, indicating that the operation is successful.

The official website also provides more cases to try: Bun Examples

Conclusion

After experience, the use of this tool needs to be improved, especially for Windows users, it is not easy to install, it is best to provide a Windows installation package. It is currently only a beta version, which can be used for learning. There may still be a long way to go before it can be used in production. After all, Node.js has been well known by front-end development, and the ecology of Deno is still being established. There is still a long way to go.

Reference

Latest comments (0)

Visualizing Promises and Async/Await 🤯

async await

☝️ Check out this all-time classic DEV post