— Nobody said that
Where were we? Aha, you had question and I told you that I will answer your question later. Now, it is time to answer your question. Let me first copy your question here so we both know what the question was.
I have not invoked,
console object and still
.log() method works like charm, but why not not
.platform(). Both are the part of standard library of Node, aren't they?
We tried but,
.platform() method doesn't work until we use
os module. Let me surprice you more with another method. Create a new file with name
process.js and write down following code.
// A simple program that give current working directory ( cwd ). var cwd = process.cwd(); console.log(cwd);
If you run this program -
$ node process /home/kiran/Node
So, I have invoke,
.cwd() method from
process module and saved the return value into variable called
cwd and then
consoled out it with
console.log(). And we get the output, even if we haven't
Now it is time to revealed that secret.
process modules are worked without
require() because they are the part of
globals. Yes, whatever methods or constants or classes are defined here inside of globals can be accessible without explicit mentioned of
require() method. Actually,
require() itself under globals category. Hence, we are directly using them. So, in future while reading someone else code, if you find
process.env don't be amazed.
So, while we are here in globals module, why not write one more program demonstrate the usage of other global components.
Let's create a new file called
globals.js and write the following comment inside of it.
// A simple program that give current directory and file name using varaibles.
Actually, instead of
process.cwd() you can also use
__dirname for getting current working directory. You can also use
__filename to get current file name. Let's write these inside of
console.log() so we can printed out them in terminal.
// A simple program that give current directory and file name using variables. console.log(__dirname); console.log(__filename);
Running this program give,
$ node globals /home/kiran/Node/src /home/kiran/Node/src/globals.js
I hope now you understand that there is nothing magic or exceptional. We are not difining these objects because they are globals. And now you know that, those objects which are part of standard library but not listed in global needs to be define using
That's is for the globals and while we are in standard library, we are going to explore few other modules as well before we dive into something serious like creating a web server!
See you in next article!
Originally posted on my blog.