Let's start this article from where we left off in previous one -
hello, world program. Let me copy that previous
hello.js program here so we can discuss further -
// A simple hello, world program console.log("hello, world");
This program is an identical to browser's hello, world program. You know,
console, you know
log() and you know
//. If you don't know then let me tell you that,
.log() is a method within a
console object. Hence, we have written
console.log() meaning, we are using
.log() method from
// is used to write a single line of comment. You can use,
os.platform() will work because it is mentioned on Node's Doc under
Why not write one program and confirm this method. Create a new program called
os.js and write the following comment first.
// A program to demonstrate .platform() method from os module.
Now, let's use,
.platform() method from
os module as below -
// A program to demonstrate .platform() method from os module. os.platform();
From the docs, you can confirm that this
.platform() method doesn't require any parameter(s). Hence, I have just written an empty
(). I think, out program is ready to run.
$ node os /home/kiran/Node/src/os.js:3 os.platform() ^ ReferenceError: os is not defined at Object. (/home/kiran/Node/src/os.js:3:1) at Module._compile (module.js:649:30) at Object.Module._extensions..js (module.js:660:10) at Module.load (module.js:561:32) at tryModuleLoad (module.js:501:12) at Function.Module._load (module.js:493:3) at Function.Module.runMain (module.js:690:10) at startup (bootstrap_node.js:194:16) at bootstrap_node.js:666:3
Woh! Looks like from the error, Node doesn't recognize
os and complain that
os is not defined.
We are getting this error, because although it is define within a standard library, but by default it is not available to use. You need to include this
os module in this program using
require method. How to include? Just write the following line at the top of our program.
var os = require("os");
I want to invoke
os standard library of module, so I have passed as argument to
require() method and save it as an object called
os as we mentioned with
var os. You can give any name you want to give. But, giving same name as what we have for standard module is normal practice.
Adding this line in our
// A program to demonstrate .platform() method from os module. var os = require("os"); os.platform();
Now, let's run this program.
$ node os
What happen? Our program run but we don't give any output. Why? Because, as docs mentioned for
The os.platform() method returns a string identifying the operating system platform as set during compile time of Node.js.
Meaning that it return string result not print it. Printing is a part, that we have to add and we know one method to print something on terminal, right? Yeah,
console.log() method. So, wrapping this,
// A program to demonstrate .platform() method from os module. var os = require("os"); console.log(os.platform());
Now, we are finally done with
os.js program. I promise, our program will run and give the result.
$ node os linux
Yeee! I got
linux because I am using GNU/Linux. You might get something else based on you operating system.
If you have sharp eye and really pay attention to details then you may have question like, I have not invoke,
console object and still
.log() method works like charm, but why not
.platform() method? Both are the part of standard library of Node, aren't they?
They are and your question has valid point.
Let's discuss, this in next article, because it is time to revise what you have learn in this article as we have introduce couple of new concepts!
See you in next article.
Originally published at my blog.