Bun — Detect the Version at Runtime

Sometimes you need to detect the version running your code at runtime. You may need this to ensure that your code runs with a required version of Bun. This tutorial shows you how to detect the Bun version, that runs your code, at runtime.

Bun Series Overview

Determine the Bun Version Running Your Code

Bun strives for feature parity with Node.js. It provides the same API as Node.js core modules and emulates their behavior. That means, you can access the process.versions object. This version object contains an entry for the bun property if you’re running your code in a Bun environment:

const bunVersion = process.versions.bun  
console.log(`Your Bun version is: ${bunVersion}`)  

Running the snippet with Bun prints the version number:

$ bun bunVersion.js
'Your Bun version is: 1.1.8'  

Running the snippet with Node.js will also work, but the Bun version won’t be available:

$ node bunVersion.js
'Your Bun version is: undefined'  

We have a related tutorial here on Future Studio that could be of interest for you: [how to detect the Node.js version at runtime](https://futurestud.io/tutorials/node-js-determine-the-node-js-version-running-your-script
).

Using Bun.version

Bun provides a global Bun object in their environment. If you’re running your code with Bun, you could use that global directly:

Bun.version  
// 1.1.8
Be careful using the `Bun` global if you might run your code with other runtimes, like Node.js. The `Bun` global won’t be available in a Node.js environment and your code will break if you try to access `Bun.version`.  

That’s it!


Mentioned Resources

Explore the Library

Find interesting tutorials and solutions for your problems.