Node.js Global functions & objects | Trickcode

Node.js Global functions & objects,global Variable,require function,console function,__filename,__dirname,process object,Node.js
Share it:
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 it:

Nodejs

Post A Comment:

0 comments: