Computer scienceBackendNode.jsCore ConceptsInternal modulesOther modules

Introduction to os module

5 minutes read

As an active computer user, you need to know basic information about your computer, mainly about the computer's operating system and hardware. This info may be helpful when installing a new program or for troubleshooting purposes. For example, the information about an operating system's name, memory, CPU, network devices, and many more. You have probably already tried to get this data in your Control Panel or in the terminal (cmd, bash). With the os module, we can do the exact same thing and use it to our benefit when creating web and mobile apps.

What is an os module?

The os module is an inbuilt module of Node.js that provides information about the operating system on which the program is running. As developers, we may want to check how much free memory a user's computer has or which platform it is running on — Windows, Linux, or macOS. Another example: we are building a heavyweight app that requires our users' OS to run on their latest versions. Prior to installation, we can easily access that data and accept or deny further installation processes based on the results.

Let's bring it into our app and start using it. Simply write const os = require('node:os'); at the top of your file.

OS module methods

Now we can learn about the main methods that os module offers to us. Let's divide them into three groups: methods related to CPU, operating system, and memory.

CPU methods:

  • os.arch()

Returns the operating system CPU architecture. Possible values are 'arm', 'arm64', 'ia32', 'mips', 'mipsel', 'ppc', 'ppc64', 's390', 's390x', 'x64'.

  • os.cpus()

Provides information about each logical CPU core of the computer. It is an array of objects, each of which contains data about the model, the speed of the CPU core, and time in milliseconds spent in different modes (user, sys, idle, and so on).

Here's what we got for these two methods.

Input:

const os = require('node:os');

console.log(os.arch());
console.log(os.cpus());

Output:

Result output of os.arch method with the content – x64, and os.cpus method results with an array of two objects that contain information about CPU

Operating system-related methods:

  • os.type()

The os.type() method returns the operating system's name. For instance, it returns 'Linux' on Linux, 'Darwin' on macOS, and 'Windows_NT' on Windows.

  • os.platform()

Returns a string identifying the operating system platform, for example, 'darwin', 'aix', 'win32', and so on.

  • os.version()

Gives information about the operating system's version (Windows 10, Windows 8, macOS Monterey, macOS Big Sur, and others).

  • os.release()

Outputs the operating system's version number.

Memory-related methods:

  • os.totalmem()

Returns total memory bytes.

  • os.freemem()

Returns free memory bytes. The numbers in the output are too long. We can divide it by 1024 three times to get the values in GB.

You may try this code out to see your OS specs:

const os = require('node:os');

console.log("OS name is " + os.type());
console.log("OS platform is " + os.platform());
console.log("OS version is " + os.version());
console.log("OS release is " + os.release());
console.log("Total memory is " + os.totalmem() / Math.pow(1024, 3) + " GB");
console.log("Free memory is " + os.freemem() / Math.pow(1024, 3) + " GB");

We went through some of the methods of the os module. However, there are a few more, for example, os.userInfo(), os.homedir(), os.hostname(), os.networkInterfaces(). You may learn more about them in the official documentation of Node.js.

OS module constants

The os module also gives access to constants that are specific to the current operating system. Among them are signal constants, dlopen constants, priority constants, and constants for error codes. Let's return all of them by typing the following code:

const os = require('node:os');

console.log(os.constants);

It will return an object with all the available constants. However, if you would like to only see signal, priority, or error constants, you may invoke os.constants.signals, os.constants.priority, or os.constants.errno commands, respectively. Please, note that not all constants may exist in some operating systems.

The list might look unfamiliar and intimidating, but do not panic! You will not need all of them as a beginner programmer. For curiosity reasons, feel free to check the whole list in the Node.js Docs. Here are some of the constants:

  • SIGINT is a signal that is sent when a user wants to terminate the process. You've probably interacted with it when you canceled a program in the terminal by running Ctrl + C command.

  • SIGILL is a signal that indicates an illegal or malformed instruction.

  • EACCES is an error code that tells that the operation does not have sufficient permissions.

  • ENOTSUP is an error code showing that the operation is not supported.

Conclusion

Now that we have learned about the os module, you may use it to get information about an operating system and hardware. It is possible to access data like the operating system's name, its version, total and free memory, user info, the operating system's constants, and many more. The os.constants provides a reference to four types of constants: error handling constants, signals, dlopen constants, and priority constants.

9 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo