AdBlock Detected

We provide high-quality source code for free. Please consider disabling your AdBlocker to support our work.

Buy me a Coffee

Saved Tutorials

No saved posts yet.

Press Enter to see all results

Node.js Global functions & objects | Trickcode

By pushpam abhishek
Listen to this article
Node.js Global functions & objects
google

Node.js
has the following global functions and objects, these functions/objects can be accessed from anywhere without requiring any module.


Node.js Global functions & objects:
  • global Variable
  • require function
  • console function
  • __filename
  • __dirname
  • process object

1).global

variables declared as “global” is accessed in all the modules.
It is similar to “var” declared in the global scope. Bur “var” variables are not accessed from outside the module.global x;


2).require()

require function is used to load a module.
var fs = require('fs');
//we can call the API in 'fs' module using fs object.

fs.watchFile('message.text', function (curr, prev) {
  console.log('the current mtime is: ' + curr.mtime);
  console.log('the previous mtime was: ' + prev.mtime);
});

fs.exists('/etc/passwd', function (exists) {
if(exists)
console.log('YES');
else
console.log('NO');
});


3).console

console object is used to print stdout and stderr.


console.log("my name is Ravishanker"); console.info("my name is Ravishanker"); console.warn("my name is Ravishanker"); console.error("my name is Ravishanker");



4).__filename

__filename is the absolute path of the file being executed
console.log(__filename);


5).__dirname
__dirname is the name of the directory that the currently executing script resides in.



console.log(__dirname);



6).process

the process is a global object gives details of the current process and provides functions to control the process.


console.log(process.execPath);
process.abort();
console.log('This process pid = ' + process.pid);

Share this post

pushpam abhishek

About pushpam abhishek

Pushpam Abhishek is a Software & web developer and designer who specializes in back-end as well as front-end development. If you'd like to connect with him, follow him on Twitter as @pushpambhshk

Comments