The os module allows us to get 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, the 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('Host Name:', 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 Info:', os.userInfo());
console.log('Current User Home Directory:', 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 get details such as IP addresses and interface names.
import os from 'node:os';
const interfaces = os.networkInterfaces();
console.log('Network Interfaces:', interfaces);
Get system uptime information
With the os module, we can also get information about the system uptime, which tells us 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 us with access to some system constants, such as the default end-of-line and default directories.
import os from 'node:os';
console.log('Default Line Ending:', os.EOL);
console.log('Node Process Execution Directory:', os.homedir());
console.log('Default Temporary Directory:', os.tmpdir());
Download the code
All the code from this post is available for download on Github
