ubuntu-version

Get Ubuntu version from current system

Usage no npm install needed!

<script type="module">
  import ubuntuVersion from 'https://cdn.skypack.dev/ubuntu-version';
</script>

README

ubuntu-version

npm CI codecov badge

ubuntu-version is a tiny Node.js package to get Ubuntu version from system.

Installation

npm install --save ubuntu-version

Example code

const { getUbuntuVersion } = require('ubuntu-version');

(async () => {
    const version = await getUbuntuVersion();

    if (version.length === 0) {
        throw new Error('This OS is not Ubuntu');
    }

    console.log(version); // e.g. [18, 4, 2] for Ubuntu 18.04.2 LTS
})().catch(console.error);

This package also supports TypeScript.

Why not use os.release?

Because it is not useful for detecting Ubuntu version in GitHub Actions.

For example:

> Run node -e 'console.log(require("os").release())'
5.4.0-1036-azure

Instead, we need to get OS information from lsb_release command.

The API

function getUbuntuVersion(): Promise<number[]>;

getUbuntuVersion is a function to get Ubuntu version as an array of versions.

Returned array has 2 or 3 elements. The first and second elements represetnt major and minor versions. When the third element exists, it represents patch version. For example, on Ubuntu 16.04.1 LTS, this function will return a promise resolved as [16, 4, 1].

When this function is called on OSes other than Linux, when lsb_release command shows that it is not Ubuntu or when lsb_release command is not found, the returned Promise value will be resolved as [].

When running lsb_release command failed with non-zero exit status, the returned Promise value will be rejected with an Error object which describes how the command failed.

License

Distributed under the MIT license.