Obtaining the OPERATING System

It is easy to determine the operating system in Node.js. Process. platform returns a string that identifies the operating system platform

  • aix
  • darwin
  • freebsd
  • linux
  • openbsd
  • sunos
  • win32

In addition to this method, you can also use the OS.platform () method of the OS module to obtain the same result.

Obtain the Windows system version number

After knowing the operating system, we also want to obtain its version number, for example, if the user is Windows, I want to know he is using Win7 or Win10 ah, this time should do? The OS module’s os.release() method is used to obtain the following format:

10.0.18363
Copy the code

The format is major.minor.build. The mapping between versions is as follows:

Version major.minor ------------------------------------------ ------------- Windows 10, Windows Server 2016 10.0 Windows 8.1, Windows Server 2012 R2 6.3 Windows 8, Windows Server 2012 6.2 Windows 7, Windows Server 2008 R2 6.1 Windows Vista, Windows Server 2008 6.0 Windows XP Professional X64 Edition, Windows Server 2003, Windows Home Server Windows XP 5.1 Windows 2000 5.0Copy the code

For more details, please refer to the official documentation. Here gives a how to judge win7 or Win7 and below code:

const os = require('os')
const semver = require('semver')
const platform = os.platform()
const isWindows = platform === 'win32'
const release = os.release()
const isWin7 = isWindows && release.startsWith('6.1')
const win7orOlder = isWindows && semver.lte('6.1')
Copy the code

Obtain the Mac system version number

On a Mac, os.release() is incorrect. For example, my Mac version is 11.1, but OS.release () returns 20.2.0. If my Mac version is 11.5, it returns 20.5.0, so I can’t use this method. However, there is a command sw_vers on the Mac, and we run it on the terminal as follows:

$SW_vers ProductName: macOS ProductVersion: 11.4 BuildVersion: 20F71Copy the code

You can see that the ProductVersion line shows the exact version number, which can be extracted with the following command:

$ sw_vers -productVersion
11.4
Copy the code

At this point, the code appears:

const { execSync } = require('child_process')
const macVersion = execSync('sw_vers -productVersion', { encoding: 'utf-8' })
Copy the code

For the mapping between Mac version numbers, see the official documents.