The os
module allows us to obtain basic information about the operating system on which our application is running.
We can access data such as the platform, architecture, operating system version, total and free memory, temporary directory, and more.
Examples of using the OS module
Get system information
import os from 'node:os';
console.log('Platform:', os.platform());
console.log('Architecture:', os.arch());
console.log('OS version:', os.version());
console.log('Total memory (bytes):', os.totalmem());
console.log('Free memory (bytes):', os.freemem());
console.log('Temporary directory:', os.tmpdir());
console.log('Hostname:', os.hostname());
console.log('CPU:', os.cpus());
Get information about system users
We can also use the os
module to get information about system users, such as the current user and their home directory. Let’s see an example:
import os from 'node:os';
console.log('Current user information:', os.userInfo());
console.log('Home directory of the current user:', os.homedir());
Get information about network interfaces
The os
module allows us to access information about the network interfaces available on the system. We can obtain details such as IP addresses and interface names.
import os from 'node:os';
const interfaces = os.networkInterfaces();
console.log('Network interfaces:', interfaces);
Get information about system uptime
With the os
module, we can also get information about the system uptime, which indicates how long the system has been running since its last reboot.
import os from 'node:os';
console.log('System uptime (seconds):', os.uptime());
Get information about system constants
In addition to dynamic system information, the os
module provides access to some system constants, such as the default end of line and default directories.
import os from 'node:os';
console.log('Default end of line:', os.EOL);
console.log('Node process execution directory:', os.homedir());
console.log('Default temporary directory:', os.tmpdir());