Node.js — Detect if Running on macOS

Sometimes you need to detect the platform that runs your Node.js process. You can do that by using Node.js’ built-in os module. It provides a method returning the operating system and you can then compare the platform against macOS.

Node.js Series Overview

Check if the Operating System is macOS

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.

One of the values returned by Os.platform is darwin. That’s the identifier you need to compare against to detect whether Node.js runs on macOS.

Here’s a sample function determining whether Node.js is running on macOS:

import Os from 'os'

 * Determine whether the Node.js process runs on macOS.
 * @returns {Boolean}
function isMacOs () {  
  return os.platform() === 'darwin'

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

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

That’s it!

Mentioned Resources

Explore the Library

Find interesting tutorials and solutions for your problems.