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 information about the system
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 available network interfaces 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 obtain 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 access to some system constants, such as the default line ending 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