Node.js — Detect if Running on Windows

Sometimes it’s necessary to detect the operating system running the Node.js process. Node.js comes with the built-in os module providing methods to detect the current platform. Use the methods to create a utility function detecting whether you’re currently running Node.js on Windows.

Node.js Series Overview

Check if the Operating System is Windows

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 identifiers is win32. That’s the identifier for Windows. You can now compare the current platform against the win32 identifier detecting if the current platform is Windows:

import Os from 'os'

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

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.