os — 操作系统信息
2026/7/16小于 1 分钟
os — 操作系统信息
os 模块提供与操作系统交互的方法。
import os from 'os'
系统信息
| 方法 | 说明 |
|---|---|
os.arch() | CPU 架构:'x64', 'arm', 'arm64' |
os.platform() | 平台:'win32', 'linux', 'darwin' |
os.release() | 操作系统版本号 |
os.hostname() | 主机名 |
os.homedir() | 用户主目录 |
os.tmpdir() | 系统临时目录 |
os.uptime() | 系统运行时间(秒) |
os.type() | 操作系统名:'Linux', 'Windows_NT', 'Darwin' |
os.version() | 操作系统版本 |
CPU 信息
os.cpus()
// [{ model: 'Intel(R) Core(TM)...', speed: 2400, times: { user, nice, sys, idle, irq } }, ...]
os.arch() // CPU 架构
os.loadavg() // 平均负载(Linux/macOS)
内存信息
os.freemem() // 空闲内存(字节)
os.totalmem() // 总内存(字节)
网络信息
os.networkInterfaces()
// { eth0: [{ address: '192.168.1.2', netmask: '255.255.255.0', family: 'IPv4', mac: 'xx:xx:xx:xx:xx:xx' }], ... }
用户信息
os.userInfo()
// { uid: 1000, gid: 1000, username: 'joe', homedir: '/Users/joe', shell: '/bin/zsh' }
常量
os.EOL // 换行符: '\n' (POSIX) 或 '\r\n' (Windows)
