0
0
Node.jsframework~5 mins

os.platform and os.arch in Node.js

Choose your learning style9 modes available
Introduction

These functions help you find out what type of computer and operating system your program is running on. This is useful to make your program work well on different machines.

You want to run different code depending on the operating system (like Windows or Linux).
You need to know if the computer is 32-bit or 64-bit to load the right files.
You are writing a tool that behaves differently on Mac, Windows, or Linux.
You want to log system details for debugging or support.
You want to check compatibility before running certain features.
Syntax
Node.js
const os = require('os');

const platform = os.platform();
const architecture = os.arch();

os.platform() returns a string like 'win32', 'linux', or 'darwin' (Mac).

os.arch() returns the CPU architecture like 'x64' or 'arm64'.

Examples
This prints the operating system platform.
Node.js
const os = require('os');
console.log(os.platform());
This prints the CPU architecture.
Node.js
const os = require('os');
console.log(os.arch());
This runs code only if the system is Windows.
Node.js
const os = require('os');
if (os.platform() === 'win32') {
  console.log('Running on Windows');
}
Sample Program

This program prints the current system's platform and CPU architecture.

Node.js
const os = require('os');

console.log(`Platform: ${os.platform()}`);
console.log(`Architecture: ${os.arch()}`);
OutputSuccess
Important Notes

The exact output depends on the computer you run this on.

Common platforms: 'win32' (Windows), 'darwin' (Mac), 'linux' (Linux).

Common architectures: 'x64' (64-bit), 'arm64' (ARM processors).

Summary

os.platform() tells you the operating system.

os.arch() tells you the CPU type.

Use these to make your Node.js programs adapt to different computers.