Bun Usage Practices & Experience Sharing

Bun Usage Practices & Experience Sharing

preamble

Recently working on a new project: a name given by AI:echo-trails

“echo” can symbolize the reverberation of memories, where past experiences hover over these “trails” like echoes, and memories can be heard whenever they are walked over.

After finishing it and then put it out as open source, you can guess what the project is if you are interested!

Thinking about using the newer technology stacks of the day for a new project, the runtime portion of the program was chosen as theBun“all-in-one toolkit”。

Below is a sharing of the problems encountered and the solutions.

Installation Bun Mirror Source Switching

The first problem is installing Bun.

Officially, a one-click installation shell is given, but there are well-known network reasons why the installation doesn’t always go smoothly.

sh

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

Of course, if you already have a Node.js environment, you can install it directly with npm, so you don’t need to read this section:

sh

npm install -g bun

I have a local 🪜, the installation is relatively smooth, but on the server side of the words has been timeout, the following how to solve the problem:

① Acquisition of installation scripts

interviewshttps://bun.sh/installSave the script, for example by callingbun_install.sh

If you get stuck at this step you can go to the official GitHub repository and get the installation script:src/cli/install.sh

② Modify the image source to get the installation package

Reading the shell script reveals that125 – 129 行There is the following code:

sh

if [[ $# = 0 ]]; then 
    bun_uri=$github_repo/releases/latest/download/bun-$target.zip 
else 
    bun_uri=$github_repo/releases/download/$1/bun-$target.zip 
fi

You can see that the installer is downloaded from GitHub Releases by default.

All we need to do is change the value of this variable to the address on the mirror source.

sh

# 在上述脚本代码后加入 
bun_uri=镜像源地址

Here you can use Taobao’s binary file mirror source:https://registry.npmmirror.com/binary.html

for examplebun-v1.1.36

Which file should I choose?

In the context of the script, you can see that the core logic for confirming the version is as follows

sh

platform=$(uname -ms) 
 
case $platform in 
'Darwin x86_64') 
    target=darwin-x64 
    ;; 
'Darwin arm64') 
    target=darwin-aarch64 
    ;; 
'Linux aarch64' | 'Linux arm64') 
    target=linux-aarch64 
    ;; 
'MINGW64'*) 
    target=windows-x64 
    ;; 
'Linux x86_64' | *) 
    target=linux-x64 
    ;; 
esac

We just need to rununame -mscommand, and then just select the corresponding installation package based on the output.

Learned another hand!

Once you have found the corresponding version, just copy the corresponding address and fill it back in.

sh

bun_uri=https://registry.npmmirror.com/-/binary/bun/bun-v1.1.36/bun-linux-x64.zip

Finally, execute the installation script manually

sh

bash bun_install.sh

npm mirror source switching

reference articleOverride the default npm registry for bun install

establishbunfig.tomlfile, which reads as follows:

toml

[install] 
# 设置为淘宝源 
registry = "https://registry.npmmirror.com"

after thatbun installIt’s almost up.

monorepo

Bun also has a built-in solution for monorepo workspace management.Configuring a monorepo using workspaces with Bun

Just configure it in package.jsonworkspacesfield, just specify the subpackage directory.

json

{ 
  "workspaces": [ 
    "packages/*" 
  ] 
}

The same goes forworkspaceprotocols

json

{ 
  "name": "stuff-b", 
  "dependencies": { 
    "stuff-a": "workspace:*" 
  } 
}

ultimate

Other potholes encountered later have continued to be documented here as well.