Process.platform and Process.arch
You'll learn how to detect the operating system and CPU architecture your Node.js code is running on, enabling you to write platform-specific logic.
Why This Matters
Not all operating systems behave the same way. File paths use different separators (\ on Windows, / on Linux/Mac), executables have different extensions (.exe vs none), and system commands vary. Similarly, CPU architectures (x64, ARM, etc.) may require different binaries or optimization strategies. Node.js gives you process.platform and process.arch to adapt your code accordingly.
Process.platform
process.platform returns a string identifying the operating system:
'win32'— Windows (both 32-bit and 64-bit)'darwin'— macOS'linux'— Linux'freebsd','openbsd','aix'— Other Unix-like systems
console.log(process.platform); // e.g., 'darwin' on a Mac
if (process.platform === 'win32') {
console.log('Running on Windows');
} else {
console.log('Running on Unix-like system');
}
Process.arch
process.arch returns the CPU architecture Node.js was compiled for:
'x64'— 64-bit Intel/AMD'arm'or'arm64'— ARM processors (common in mobile, Raspberry Pi, new Macs)'ia32'— 32-bit Intel
console.log(process.arch); // e.g., 'arm64' on M1 Mac
if (process.arch === 'x64') {
console.log('Using 64-bit Intel/AMD processor');
}
Practical Use Case
You might use these to conditionally load platform-specific native modules or construct correct file paths:
const isWindows = process.platform === 'win32';
const separator = isWindows ? '\\' : '/';
Key Takeaway: Use process.platform and process.arch to detect the OS and CPU architecture, allowing your Node.js code to adapt behavior across different environments.