DEV Community

loading...

Developing SSG using Node: Standard Library or Modules

Kiran Chauhan
I design software with and for people.
・5 min read

This is the fourth article in series of Developing SSG using Node.

But, be careful! Not everything that you know in JavaScript will run on Node. For example, if you use document.getElementById(), Node will give an error. This method will not work on Node because Node does not have anything related to document as document is the concept of browser. But, Node have file system, Node have something related to operating system and Node have also methods for CPU's process. In general, Node have concept of standard library that you can include within your Node program. Node have many standard libraries and you can find the complete list at the documentation page.

If you open this docs page, you will find a big list with names such as, Buffer, Console, Crypto, DNS and so on. These all are the name of standard libraries come with Node. But, Node have different name for standard libraries. Node use modules work to mentioned them. Module is the Unix word meaning the block that contains code related to one particular problem. In future, you will heard word package. It is similar to module but might contains more than one module.

While we are on docs page, if you scroll through it, you will find an entry for os or operating system. If you click on it, you will again get the list, which contains the name of methods that os module has.

So, this is the normal process you have to follow in-order to find a particular method. You first have to open the documentation page, then you have to choose the module you are looking for and then finally you have to click on the method you are looking for!

For example, I want to know the hostname of my operating system. To solve this program, I fist have to open the docs page, then I have to open os module page and finally I have to search for hostname and that take me to the os.hostname. Most of the method names are self explained. So, you have just find for right keyword!

Let's create a new program and save it as - hostrider.js and write the basic comment inside of it -

// file name: hostrider.js

Now, how to use this hostname() method? The usage of method is written as header! Meaning that we have to write os.hostname() and that will returns the hostname of the operating system as a string. Pay the attention, it says return a string not print! In-order to print we need to wrap this hostname() with console.log() So, our program become,

// file name: hostrider.js

console.log(os.hostname());

That's it! Let's run this program and get the name of our host!

node hostrider.js
os.hostname();
^

ReferenceError: os is not defined
    at Object.<anonymous> (/home/kiran/downstream/noding/hostrider.js:3:1)
    at Module._compile (internal/modules/cjs/loader.js:702:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
    at Module.load (internal/modules/cjs/loader.js:612:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
    at Function.Module._load (internal/modules/cjs/loader.js:543:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
    at startup (internal/bootstrap/node.js:238:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)

Woh! What happen? Error says that, I don't know this os gentleman i.e. os is not defined. But, how this can be happen? I mean previously we have used console.log() method and Node know the console! But why not os?

Let's make this question bit more interesting by writing one more program called current-addres.js with following code,

// file name: current-address.js

const current_address = process.cwd();
console.log(current_address);

In this program, I am using cwd() method from process module and it return the string, that gives the current working directory at where Node process! Again it returns the string. So, I have placed it inside of console.log(). And now if I run this program it will give me,

$ node current-address.js
/home/kiran/downstream/noding

See! We get the output. So, console works, process works but os doesn't. Why?

Because of globals! Yes, globals. Remember, document? In JavaScript, you don't have to define what document is, because it is already there by browser as global for you. Similarly, you don't have to define, console and process because they are globals by Node and whatever define as global by Node, can be used directly within a Node program. That is the reason why console.log() works and that is also the reason why process.cwd() works. But, os.platform() doesn't work because, although, os is a part of Node's standard module, it is not part of globals. So, if something is not globals you need to include it. Or you need to required it by require() function. For example, os is not available as globals so we need to required it ( or include it ) using,

const os = require("os");

Meaning that I have included os module using this require() function and save the reference of this module in constant object called os. Now, I can access all the code which is written inside of this os module using constant os.

You can use any name you would like for the constant while requireing it. But, it is the normal practice to give a same name as what module name have to make program more readable. You can also define it with other name such as chilly_powder but that will make your program spicy in term of reading! Also, this os object save the reference to os module and later in program we never what it to reference any other modules. Hence, I have define it with const.

Let's required the os module in our hostrider.js program.

// file name: hostrider.js

const os = require("os");
console.log(os.hostname());

Now we you run this program as,

$ node hostrider.js
'ubuntu'

Yeee! Our program run and we know the hostrider - ubuntu!

Also, this require() function is part of globals. And that is the reason we have used it directly without any type of declarations.

While we are in globals, I want you to pay attention on couple of constants such as __dirname and __filename. Because, we will going to use these variables a lot, specially __dirname. It is just a constant that contains the folder name of current module.

So, if I create a new program called present-address.js and write the following code inside of it,

// file name: present-address.js

console.log(__dirname);

And if I run this program, I will get -

$ node present-address.js
/home/kiran/downstream/noding

And this is the path of current folder, in which present-address.js resides, hence present address of it!

Discussion (0)