Node.js os module All In One
Node.js os module All In One
Node.js
v12.4.0
Node.js os
Module
// ESM
import os from 'node:os';
// ❓what is the difference
import os from 'os'
// CJS
const os = require('node:os');
https://nodejs.org/api/os.html
demos
ESM
.mjs
/ package.json"type": "module"
+.js
import os from 'node:os'
// const os = require('node:os');
// console.log(`os =`, os)
// console.log(`os.arch =`, os.arch)
/* os.arch = [Function: arch] {
[Symbol(Symbol.toPrimitive)]: [Function (anonymous)]
} */
console.log(`os.arch() =`, os.arch())
// console.log(`os.cpus() =`, os.cpus())
console.log(`os.cpus().length =`, os.cpus().length)
console.log(`os.cpus()[0] =`, os.cpus()[0])
console.log(`os.hostname() =`, os.hostname())
console.log(`os.platform() =`, os.platform())
console.log(`os.userInfo() =`, os.userInfo())
console.log(`os.version() =`, os.version())
// console.log(`os =`, os)
/*
os = {
arch: [Function: arch] {
[Symbol(Symbol.toPrimitive)]: [Function (anonymous)]
},
cpus: [Function: cpus],
endianness: [Function: endianness] {
[Symbol(Symbol.toPrimitive)]: [Function (anonymous)]
},
freemem: [Function: getFreeMem] {
[Symbol(Symbol.toPrimitive)]: [Function (anonymous)]
},
getPriority: [Function: getPriority],
homedir: [Function: __node_internal_checkError] {
[Symbol(Symbol.toPrimitive)]: [Function (anonymous)]
},
hostname: [Function: __node_internal_checkError] {
[Symbol(Symbol.toPrimitive)]: [Function (anonymous)]
},
loadavg: [Function: loadavg],
networkInterfaces: [Function: networkInterfaces],
platform: [Function: platform] {
[Symbol(Symbol.toPrimitive)]: [Function (anonymous)]
},
release: [Function: getOSRelease] {
[Symbol(Symbol.toPrimitive)]: [Function (anonymous)]
},
setPriority: [Function: setPriority],
tmpdir: [Function: tmpdir] {
[Symbol(Symbol.toPrimitive)]: [Function (anonymous)]
},
totalmem: [Function: getTotalMem] {
[Symbol(Symbol.toPrimitive)]: [Function (anonymous)]
},
type: [Function: getOSType] {
[Symbol(Symbol.toPrimitive)]: [Function (anonymous)]
},
userInfo: [Function: userInfo],
uptime: [Function: getUptime] {
[Symbol(Symbol.toPrimitive)]: [Function (anonymous)]
},
version: [Function: getOSVersion] {
[Symbol(Symbol.toPrimitive)]: [Function (anonymous)]
},
constants: [Object: null prototype] {
UV_UDP_REUSEADDR: 4,
dlopen: [Object: null prototype] {
RTLD_LAZY: 1,
RTLD_NOW: 2,
RTLD_GLOBAL: 8,
RTLD_LOCAL: 4
},
errno: [Object: null prototype] {
E2BIG: 7,
EACCES: 13,
EADDRINUSE: 48,
EADDRNOTAVAIL: 49,
EAFNOSUPPORT: 47,
EAGAIN: 35,
EALREADY: 37,
EBADF: 9,
EBADMSG: 94,
EBUSY: 16,
ECANCELED: 89,
ECHILD: 10,
ECONNABORTED: 53,
ECONNREFUSED: 61,
ECONNRESET: 54,
EDEADLK: 11,
EDESTADDRREQ: 39,
EDOM: 33,
EDQUOT: 69,
EEXIST: 17,
EFAULT: 14,
EFBIG: 27,
EHOSTUNREACH: 65,
EIDRM: 90,
EILSEQ: 92,
EINPROGRESS: 36,
EINTR: 4,
EINVAL: 22,
EIO: 5,
EISCONN: 56,
EISDIR: 21,
ELOOP: 62,
EMFILE: 24,
EMLINK: 31,
EMSGSIZE: 40,
EMULTIHOP: 95,
ENAMETOOLONG: 63,
ENETDOWN: 50,
ENETRESET: 52,
ENETUNREACH: 51,
ENFILE: 23,
ENOBUFS: 55,
ENODATA: 96,
ENODEV: 19,
ENOENT: 2,
ENOEXEC: 8,
ENOLCK: 77,
ENOLINK: 97,
ENOMEM: 12,
ENOMSG: 91,
ENOPROTOOPT: 42,
ENOSPC: 28,
ENOSR: 98,
ENOSTR: 99,
ENOSYS: 78,
ENOTCONN: 57,
ENOTDIR: 20,
ENOTEMPTY: 66,
ENOTSOCK: 38,
ENOTSUP: 45,
ENOTTY: 25,
ENXIO: 6,
EOPNOTSUPP: 102,
EOVERFLOW: 84,
EPERM: 1,
EPIPE: 32,
EPROTO: 100,
EPROTONOSUPPORT: 43,
EPROTOTYPE: 41,
ERANGE: 34,
EROFS: 30,
ESPIPE: 29,
ESRCH: 3,
ESTALE: 70,
ETIME: 101,
ETIMEDOUT: 60,
ETXTBSY: 26,
EWOULDBLOCK: 35,
EXDEV: 18
},
signals: [Object: null prototype] {
SIGHUP: 1,
SIGINT: 2,
SIGQUIT: 3,
SIGILL: 4,
SIGTRAP: 5,
SIGABRT: 6,
SIGIOT: 6,
SIGBUS: 10,
SIGFPE: 8,
SIGKILL: 9,
SIGUSR1: 30,
SIGSEGV: 11,
SIGUSR2: 31,
SIGPIPE: 13,
SIGALRM: 14,
SIGTERM: 15,
SIGCHLD: 20,
SIGCONT: 19,
SIGSTOP: 17,
SIGTSTP: 18,
SIGTTIN: 21,
SIGTTOU: 22,
SIGURG: 16,
SIGXCPU: 24,
SIGXFSZ: 25,
SIGVTALRM: 26,
SIGPROF: 27,
SIGWINCH: 28,
SIGIO: 23,
SIGINFO: 29,
SIGSYS: 12
},
priority: [Object: null prototype] {
PRIORITY_LOW: 19,
PRIORITY_BELOW_NORMAL: 10,
PRIORITY_NORMAL: 0,
PRIORITY_ABOVE_NORMAL: -7,
PRIORITY_HIGH: -14,
PRIORITY_HIGHEST: -20
}
},
EOL: '\n',
devNull: '/dev/null'
}
*/
macOS
Intel CPU
s
Intel(R) Core(TM)
i7-8750H
CPU @ 2.20GHz
$ node ./os-module.mjs
os.arch() = x64
os.cpus().length = 12
os.cpus()[0] = {
model: 'Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz',
speed: 2200,
times: { user: 3276490, nice: 0, sys: 2014940, idle: 30180650, irq: 0 }
}
os.hostname() = xgqfrms-mbp.local
os.platform() = darwin
os.userInfo() = {
uid: 501,
gid: 20,
username: 'xgqfrms-mbp',
homedir: '/Users/xgqfrms-mbp',
shell: '/bin/zsh'
}
os.version() = Darwin Kernel Version 21.4.0: Mon Feb 21 20:34:37 PST 2022; root:xnu-8020.101.4~2/RELEASE_X86_64
CJS
.js
// import os from 'node:os'
const os = require('node:os');
// console.log(`os =`, os)
// console.log(`os.arch =`, os.arch)
console.log(`os.arch() =`, os.arch())
console.log(`os.cpus() =`, os.cpus())
console.log(`os.cpus().length =`, os.cpus().length)
console.log(`os.cpus()[0] =`, os.cpus()[0])
console.log(`os.hostname() =`, os.hostname())
console.log(`os.platform() =`, os.platform())
console.log(`os.userInfo() =`, os.userInfo())
console.log(`os.version() =`, os.version())
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
Top Node.js Questions
Practice problems
https://codedamn.com/problem-list/nodejs
https://codedamn.com/problem/k-NifXX_X3r46jNExN02U
refs
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17400616.html
未经授权禁止转载,违者必究!