Nodejs os module can interact with the operating system
var os = require("node:os")
serial number | API | effect |
---|---|---|
1 | os.type() | It returns on Linux 'Linux' , it returns on macOS 'Darwin' , it returns on Windows 'Windows_NT' |
2 | os.platform() | Returns a string identifying the operating system platform for which the Node.js binary was compiled. This value is set at compile time. Possible values are 'aix' , 'darwin' , 'freebsd' , 'linux' , 'openbsd' , 'sunos' , and 'win32' |
3 | os.release() | Return the version of the operating system such as 10.xxxx win10 |
4 | os.homedir() | Return to the user directory such as c:\user\xiaoman, the principle is windows echo %USERPROFILE% posix $HOME |
5 | os.arch() | The possible values for the architecture of the returned cpu are 'arm' , 'arm64' , 'ia32' , 'mips' , , 'mipsel' , 'ppc' , 'ppc64' , 's390' , 's390x' and 'x64' |
Get CPU threads and detailed information
const os = require('node:os')
os.cpus()
[
{
model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
speed: 2926,
times: {
user: 252020,
nice: 0,
sys: 30340,
idle: 1070356870,
irq: 0,
},
},
{
model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
speed: 2926,
times: {
user: 306960,
nice: 0,
sys: 26980,
idle: 1071569080,
irq: 0,
},
},
{
model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
speed: 2926,
times: {
user: 248450,
nice: 0,
sys: 21750,
idle: 1070919370,
irq: 0,
},
},
{
model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
speed: 2926,
times: {
user: 256880,
nice: 0,
sys: 19430,
idle: 1070905480,
irq: 20,
},
},
]
//.........
-
model
: Indicates the model information of the CPU, where "Intel® Core™ i7 CPU 860 @ 2.80GHz" is a specific model description. -
speed
: Indicates the clock speed of the CPU in MHz or GHz. In this case, the speed is 2926 MHz or 2.926 GHz. -
times
: is an object containing the CPU usage time, which contains the following properties:user
: Indicates the time (in milliseconds) that the CPU is used by the user program.nice
: Indicates the time (in milliseconds) that the CPU was used by lower priority user programs.sys
: Indicates the time (in milliseconds) that the CPU is used by the system kernel.idle
: Indicates the amount of time the CPU has been idle in milliseconds.irq
: Indicates the time (in milliseconds) that the CPU was used by the hardware interrupt handler.
For example, if my computer has six cores and twelve threads, it will get 12 threads
Get network information
const os = require('node:os')
os.networkInterfaces()
{
lo: [
{
address: '127.0.0.1',
netmask: '255.0.0.0',
family: 'IPv4',
mac: '00:00:00:00:00:00',
internal: true,
cidr: '127.0.0.1/8'
},
{
address: '::1',
netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
family: 'IPv6',
mac: '00:00:00:00:00:00',
scopeid: 0,
internal: true,
cidr: '::1/128'
}
],
eth0: [
{
address: '192.168.1.108',
netmask: '255.255.255.0',
family: 'IPv4',
mac: '01:02:03:0a:0b:0c',
internal: false,
cidr: '192.168.1.108/24'
},
{
address: 'fe80::a00:27ff:fe4e:66a1',
netmask: 'ffff:ffff:ffff:ffff::',
family: 'IPv6',
mac: '01:02:03:0a:0b:0c',
scopeid: 1,
internal: false,
cidr: 'fe80::a00:27ff:fe4e:66a1/64'
}
]
}
address
: Indicates the IP address of the local loopback interface, here it is'127.0.0.1'
.netmask
: Indicates the subnet mask of the local loopback interface, here it is'255.0.0.0'
.family
: indicates the address family (address family), here is'IPv4'
, indicates the IPv4 address.mac
: Indicates the MAC address of the local loopback interface, here it is'00:00:00:00:00:00'
. Note that the local loopback interface usually has no hardware involved, so the MAC address is usually all zeros.internal
: Indicates whether the local loopback interface is an internal interface, here it istrue
, indicating that it is an internal interface.cidr
: Indicates the CIDR notation of the local loopback interface, that is, the combination of the network address and the subnet mask, here'127.0.0.1/8'
, means the entire127.0.0.0
network.
the case
What is the use of knowing this information?
A very classic example, webpack vite, everyone should have used it. They have a configuration item that can open the browser. open:true
Let’s simply reproduce it.
const {
exec } = require('child_process');
const os = require('os');
function openBrowser(url) {
if (os.platform() === 'darwin') {
// macOS
exec(`open ${
url}`); //执行shell脚本
} else if (os.platform() === 'win32') {
// Windows
exec(`start ${
url}`); //执行shell脚本
} else {
// Linux, Unix-like
exec(`xdg-open ${
url}`); //执行shell脚本
}
}
// Example usage
openBrowser('https://www.juejin.cn');