![]() |
Node.js has the following global functions and objects, these functions/objects can be accessed from anywhere without requiring any module.
- 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;
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);
Post A Comment:
0 comments: