Node.js Reference ยท path, os and url
Node.js os.platform() and os.type() methods
Reference for os.platform(), os.type(), os.arch(), os.endianness() and os.devNull in Node.js: detect the operating system and write portable code.
os.platform() returns a short lowercase string naming the operating system Node.js was built for: 'linux', 'darwin' (macOS), 'win32' (Windows, including 64-bit), 'freebsd', 'aix' and a few others. It returns the same value as process.platform.
os.type() returns the name the system itself reports, such as 'Linux', 'Darwin' or 'Windows_NT'. Alongside them, os.arch() gives the CPU architecture of the Node.js build, os.endianness() returns 'LE' or 'BE', and os.devNull is the path of the null device (/dev/null or \\.\nul).
import os from "node:os"; if (os.platform() === "win32") { console.log("Windows"); }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
os.platform(): string os.type(): string os.arch(): string os.endianness(): "BE" | "LE" os.devNull: string
| Name | Description |
|---|---|
| (none) | The functions take no arguments; devNull is a property. |
Returns: strings. Possible platform() values include 'aix', 'darwin', 'freebsd', 'linux', 'openbsd', 'sunos', 'win32' and 'android'. Typical arch() values are 'x64', 'arm64', 'ia32', 'arm', 'ppc64', 's390x' and 'riscv64'.
Edge cases: on Windows platform() is 'win32' even on 64-bit systems. WSL reports 'linux'.
Usage notes
- Prefer feature checks to platform checks. Test for the thing you need (a file, a command) when you can; platform branches multiply and rot.
darwinis macOS, not "Darwin the kernel project". Do not check for'macos'or'osx'; such values never occur.- Use
pathinstead of testing for Windows when you only need separators:path.sepalready does the right thing. os.arch()describes Node.js, not the CPU. A 32-bit build on a 64-bit machine reports'ia32'. Useos.machine()for the hardware.- Cross-platform tests can stub the value by passing a custom object into your own function rather than reading
os.platform()deep inside. os.devNullis handy when you need to discard output, for example as the target of a write stream.