We now have a youtube channel. Subscribe!

Node.js | OS Module

Node.js | OS Module


Hello folks! welcome back to a new edition of our tutorial on Node.js. In this section of our tutorial on Node.js, we will be studying about Node.js OS Module.

The os module gives a few basic operating system related utility functions. OS module can be imported using the following syntax.

var os = require("os")

Methods

Following table below is the list of methods available in Node.js OS module -

Sr.No.Method & Description
1

os.tmpdir()

Returns the operating system's default directory for temp files.

2

os.endianness()

Returns the endianness of the CPU. Possible values are "BE" or "LE".

3

os.hostname()

Returns the hostname of the operating system.

4

os.type()

Returns the operating system name.

5

os.platform()

Returns the operating system platform.

6

os.arch()

Returns the operating system CPU architecture. Possible values are "x64", "arm" and "ia32".

7

os.release()

Returns the operating system release.

8

os.uptime()

Returns the system uptime in seconds.

9

os.loadavg()

Returns an array containing the 1, 5, and 15 minute load averages.

10

os.totalmem()

Returns the total amount of system memory in bytes.

11

os.freemem()

Returns the amount of free system memory in bytes.

12

os.cpus()

Returns an array of objects containing information about each CPU/core installed: model, speed (in MHz), and times (an object containing the number of milliseconds the CPU/core spent in: user, nice, sys, idle, and irq).

13

os.networkInterfaces()

Get a list of network interfaces.



Properties

The following table is the list of properties available in Node.js OS module -

Sr.No.Property & Description
1

os.EOL

A constant defining the appropriate End-of-line marker for the operating system.


Example

The following example below demonstrates a few OS methods. Create a file, main.js via the following code -

var os = require("os");

// Endianness
console.log('endianness : ' + os.endianness());

// OS type
console.log('type : ' + os.type());

// OS platform
console.log('platform : ' + os.platform());

// Total system memory
console.log('total memory : ' + os.totalmem() + " bytes.");

// Total free memory
console.log('free memory : ' + os.freemem() + " bytes.");

Now run the main.js to see the result -

$ node main.js

Output

Verify the output.

endianness : LE
type : Linux
platform : linux
total memory : 25103400960 bytes.
free memory : 20676710400 bytes.


Alright guys! This is where we are going to be rounding up for this tutorial. In our next tutorial, we will be studying about Node.js Path Module.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain