You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesión a prueba de IA

Antes: $249

Currency
$209
Suscríbete

Termina en:

2 Días
17 Hrs
46 Min
38 Seg
Curso de Fundamentos de Node.js

Curso de Fundamentos de Node.js

Oscar Barajas Tavares

Oscar Barajas Tavares

Módulo OS: información del sistema operativo en Node.js

13/20
Resources

Understanding the operating system on which our application runs is critical to developing robust and efficient software. The Node.js OS module gives us access to valuable system information that can be crucial for monitoring, optimization and compatibility. Let's discover how to implement this module to obtain relevant data from the execution environment.

How to use the OS module in Node.js?

The OS module provides utilities and information related to the operating system where our application is running. To start using it, we must first import it into our project. We create a new file called OS.js and include the module:

const os = require('os');

With this simple line, we already have access to all the functionalities offered by this module. To organize our code, we can create a function that shows the system information:

function showSystemInfo() { console.log("Operating system:", os.type()); console.log("Platform:", os.platform()); console.log("Architecture:", os.arch()); console.log("Operating system version:", os.release());}
showSystemInfo();

By running this code with node OS.js, we will get basic information about our system. For example, on a MacBook, we might see something like:

Operating system: darwinPlatform: darwinArchitecture: arm64Operating system version: 24.3.

It is important to note that the results will vary depending on the operating system you are using. If you run the same code on Windows or Linux, you will see information corresponding to those systems.

What additional information can we get?

The OS module allows us to access much more data about our system. We can extend our function to include:

function showSystemInfo() { console.log("Operating system:", os.type()); console.log("Platform:", os.platform()); console.log("Architecture:", os.arch()); console.log("Operating system version:", os.release()); console.log("Total memory:", os.totalmem()); console.log("Free memory:", os.freememem()); console.log("Available CPUs:", os.cpus().length); console.log("Home directory:", os.homedir()); console.log("Hostname:", os.hostname());}

This extended information provides us with valuable data such as:

  • The total and free memory of the system.
  • The number of available CPU cores
  • The user's home directory
  • The computer's hostname

For which use cases is the OS module useful?

The information we obtain through the OS module can be extremely useful in several scenarios:

Application monitoring

We can create tools that monitor system resource usage and alert when certain thresholds are reached. For example:

function monitorResources() { const totalMemory = os.totalmem(); const freeMemory = os.freememem(); const usedMemoryPercentage = ((totalMemory - freeMemory) / totalMemory) * 100;  
 console.log(`Memory  usage: ${usedMemoryPercentage.toFixed(2)}%`);  
 if (usedMemoryPercentage > 90) { console.log("Warning! Memory usage critical"); } }}

IoT applications

In Internet of Things devices, knowing the architecture and available resources is crucial to optimize performance:

function checkIoTCompatibility() { const cpus = os.cpus(); const architecture = os.arch(); const memory = os.totalmem() / (1024 * 1024 * 1024); // Convert to GB  
 console.log(`Devicewith ${cpus.length} cores, architecture ${architecture} and ${memory.toFixed(2)}GBof RAM`);  
 // Logic to determine what functionality to enable based on resources}

Data analysis

For data processing applications, it is important to know the available resources to optimize the workload:

function configureDataProcessing() { const availableCores = os.cpus().length; const recommendedWorkers = Math.max(1, availableCores - 1); // Leave one core free  
 console.log(`Configuringprocessing with ${recommendedWorkers} workers`);  
 // Configure the number of workers for parallel processing}

The Node.js OS module is a powerful tool that allows us to adapt our applications to the environment in which they run. Whether for monitoring, optimization or compatibility, knowing the operating system and its available resources helps us to create more efficient and robust software. Have you used this module in any of your projects? What other use cases can you think of? Share your experience in the comments.

Contributions 1

Questions 0

Sort by:

Want to see more contributions, questions and answers from the community?

esta es la información que me arroja Node.js sobre mi sistema operativo ![](https://static.platzi.com/media/user_upload/image-10892f05-719c-4822-adc8-a3f4ebba0add.jpg)