Version
22.16.0
Platform
Microsoft Windows NT 10.0.19045.0 x86
Subsystem
os
What steps will reproduce the bug?
- Run
os.machine() in node x86 REPL
How often does it reproduce? Is there a required condition?
100% reproducible
What is the expected behavior? Why is that the expected behavior?
The os.machine() returns i386
What do you see instead?
The os.machine() returns i686 which indicates the x86 app running on x64 CPU (which makes no sense for a x86 CPU host)
Additional information
The main problem - there's no way to determine the true Windows host OS architecture in NodeJS (process.arch is not about it)
Workaround
You can determine the true Windows host OS architecture using the PROCESSOR_ARCHITECTURE environment variable:
function getWindowsOsArchitectureUsingPlatform(): NodeJS.Architecture | undefined {
const osArchitectureEnvVar = this.getWindowsOsArchitectureEnvVar()
const sanitizedOsArchitectureEnvVar = osArchitectureEnvVar?.toLowerCase()
switch (sanitizedOsArchitectureEnvVar) {
// Values listed in win32 docs:
// https://learn.microsoft.com/en-us/windows/win32/winprog64/wow64-implementation-details#environment-variables
case "amd64": return "x64"
case "ia64": return "x64"
case "arm64": return "arm64"
case "x86": return "ia32"
}
return undefined
}
function getWindowsOsArchitectureEnvVar(): string | undefined {
// Despite its name, the PROCESSOR_ARCHITECTURE environment variable stores the architecture of the operating
// system, not the CPU itself (e.g. for Windows on x86 on an x86_64 processor, the variable has the value `x86`)
const osArchitectureEnvVar = process.env["PROCESSOR_ARCHITECTURE"]
const sanitizedOsArchitectureEnvVar = osArchitectureEnvVar?.toLowerCase()
// When running a 32-bit process (x86) on a 64-bit operating system (x86_64, arm64), the actual
// value of PROCESSOR_ARCHITECTURE are contained in PROCESSOR_ARCHITEW6432
// https://learn.microsoft.com/en-us/windows/win32/winprog64/wow64-implementation-details#environment-variables
const is32BitProcess = sanitizedOsArchitectureEnvVar === "x86"
if (is32BitProcess) {
const wowCpuArchitectureEnvVar = process.env["PROCESSOR_ARCHITEW6432"]
if (wowCpuArchitectureEnvVar) return wowCpuArchitectureEnvVar
}
return osArchitectureEnvVar
}
Version
22.16.0
Platform
Subsystem
os
What steps will reproduce the bug?
os.machine()in node x86 REPLHow often does it reproduce? Is there a required condition?
100% reproducible
What is the expected behavior? Why is that the expected behavior?
The
os.machine()returnsi386What do you see instead?
The
os.machine()returnsi686which indicates the x86 app running on x64 CPU (which makes no sense for a x86 CPU host)Additional information
The main problem - there's no way to determine the true Windows host OS architecture in NodeJS (
process.archis not about it)Workaround
You can determine the true Windows host OS architecture using the
PROCESSOR_ARCHITECTUREenvironment variable: