node的process.platform的值都有可能是哪些

77 阅读1分钟

在 Node.js 中,process.platform 属性用于返回当前运行程序的操作系统平台标识, 常见的可能值如下:

  • 'aix':IBM AIX 操作系统
  • 'darwin':macOS 或 iOS 系统(基于 Darwin 内核)
  • 'freebsd':FreeBSD 操作系统
  • 'linux':Linux 操作系统
  • 'openbsd':OpenBSD 操作系统
  • 'sunos':Oracle Solaris 操作系统
  • 'win32':Windows 系统(包括 32 位和 64 位)

这些值可用于在代码中根据不同操作系统执行特定逻辑,例如:

if (process.platform === "win32") {
  console.log("运行在 Windows 系统");
} else if (process.platform === "darwin") {
  console.log("运行在 macOS 系统");
} else if (process.platform === "linux") {
  console.log("运行在 Linux 系统");
}

注意:这些值是固定的字符串标识,不区分具体的版本(如 Windows 10 和 Windows 11 都会返回 'win32')。