Node.js — Detect if Running on Linux

In some situations, you need to detect the operating system running the Node.js process. You can do that by using Node.js’ built-in os module.

Node.js Series Overview

Check if the Operating System is Linux

The os module in Node.js provides a platform method. This platform method allows you to identify the operating system. It returns the platform name as a string identifier. You can then go ahead and compare the identifier against Linux.

Here’s a sample function determining whether Node.js is running on the Linux platform:

import Os from 'os'

/**
 * Determine whether the Node.js process runs on Linux.
 *
 * @returns {Boolean}
 */
function isLinux () {  
  return Os.platform() === 'linux'
}

At the time of writing this tutorial, possible platform values are:

  • aix
  • android
  • darwin
  • freebsd
  • haiku
  • linux
  • openbsd
  • sunos
  • win32
  • cygwin
  • netbsd

These values are set at compile-time and don’t change during runtime.

That’s it!


Mentioned Resources

Explore the Library

Find interesting tutorials and solutions for your problems.