Node.js在不同的架构上运行。你可以在所有常见的操作系统上使用它,如Windows、Linux和macOS。你也可以在32位和64位机器上使用它。本教程告诉你如何检测你是在32位还是64位系统中运行。
检测Node.js进程是运行在64位还是32位系统上
Node.js自带一个内置的os模块。这个os 模块提供了与操作系统相关的效用。你可以使用os.arch() 方法访问系统的架构。这将返回Node.js所运行的操作系统的CPU架构。你可以使用这个方法来确定它是在32位还是64位CPU上运行:
import Os from 'os'
/**
* Determine whether the current platform is 64 bit.
*
* @returns {Boolean}
*/
function is64Bit() {
return ['arm64', 'ppc64', 'x64', 's390x'].includes(Os.arch())
}
is64Bit()
// true
// (for example, when running on an Intel Mac x64)
在编写本教程时,以下字符串列表描述了Os.arch() 的返回值:
- arm
- arm64
- ia32
- mips
- mipsel
- ppc
- ppc64
- s390
- s390x
- x32
- x64
就这样吧!