Install Bun for Windows: A Modern JavaScript Runtime Like Node or Deno
Tags: node, bunjs, javascript, webdev
Published: 2022-07-11 07:12:35


## Introduction

Recently, a project called [Bun](https://bun.sh/) 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](https://docs.microsoft.com/en-us/windows/wsl/install)

It is recommended to use Windows Terminal to quickly open the WSL terminal: [Microsoft Store: Windows Terminal](https://www.microsoft.com/store/productId/9N0DX20HK701)

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

```shell
bcdedit /set hypervisorlaunchtype auto
```

> 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](https://lwebapp.com/en/post/vmware-vcpu-error)

## Install Bun

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

```shell
error: unzip is required to install Bun (see: https://github.com/Jarred-Sumner/bun#unzip-is-required
```

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

```shell
sudo apt-get install unzip
```

2.Enter under WSL

```shell
 curl https://bun.sh/install | bash
```

The following prompt appears, the installation is successful

```shell
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"
```

3.Manually add environment variables as prompted

Open the `.bashrc` file with vim

```shell
vim /root/.bashrc
```

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

```shell
BUN_INSTALL="/root/.bun"
PATH="$BUN_INSTALL/bin:$PATH"
```

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

```shell
bun -h
```

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](https://docs.microsoft.com/en-us/windows/wsl/tutorials/wsl-vscode)

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

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

start http service

```shell
bun run http.js
```

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](https://github.com/oven-sh/bun/tree/main/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

- [Install Bun for Windows: A Modern JavaScript Runtime Like Node or Deno](https://lwebapp.com/en/post/bun-windows)
- ["VMware Workstation Unrecoverable Error: (vcpu-1)" Troubleshooting for Vmware Workstation16](https://lwebapp.com/en/post/vmware-vcpu-error)
- [Bun](https://bun.sh/)