Skip to main content

Process Object in Node JS


             Node JS offers several Inbuilt core objects. Here we gonna discuss about one of the important Core Object - Process.

The process object is a global object and can be accessed from anywhere. Here I have listed out few Methods and properties of an Object.

Process Events

Event: 'exit'
Emitted when the process is about to exit. There is no way to prevent the exiting of the event loop at this point, and once all exit listeners have finished running the process will exit. Therefore you must only perform synchronous operations in this handler.

process.on('exit', function(code) {
// Sync function only
    
console.log('About to exit with code:', code);
});

Event: 'beforeExit'
This event is emitted when node empties it's event loop and has nothing else to schedule. Normally, node exits when there is no work scheduled, but a listener for 'beforeExit' can make asynchronous calls, and cause node to continue.

'beforeExit' is not emitted for conditions causing explicit termination, such as process.exit() or uncaught exceptions, and should not be used as an alternative to the 'exit' event unless the intention is to schedule more work.


Event: 'uncaughtException'
Emitted when an exception bubbles all the way back to the event loop. If a listener is added for this exception

process.on('uncaughtException', function(err) {
  
console.log('Caught exception: ' + err);
});

setTimeout(
function() {
  
console.log('This will still run.');
},
500);

// Intentionally cause an exception, but don't catch it.
nonexistentFunc();
console.log('This will not run.');

process.argv

An array containing the command line arguments. The first element will be 'node', the second element will be the name of the JavaScript file. The next elements will be any additional command line arguments.


process.execPath

This is the absolute pathname of the executable that started the process.
C:\Program Files\nodejs\node.exe


process.cwd()

Returns the current working directory of the process.
console.log('Current directory: ' + process.cwd());

process.chdir(directory)

Changes the current working directory of the process or throws an exception if that fails.
console.log('Starting directory: ' + process.cwd());
try {
  process.chdir(
'/tmp');
  
console.log('New directory: ' + process.cwd());
}
catch (err) {
  
console.log('chdir: ' + err);
}

process.env

An object containing the user environment.


process.exit([code])

Ends the process with the specified code. If omitted, exit uses the 'success' code 0.
To exit with a 'failure' code:
process.exit(1);
The shell that executed node should see the exit code as 1.

process.exitCode

A number which will be the process exit code, when the process either exits gracefully, or is exited via process.exit() without specifying a code.
Specifying a code to process.exit(code) will override any previous setting of process.exitCode.

process.version
A compiled-in property that exposes NODE_VERSION.
console.log('Version: ' + process.version);
process.versions
A property exposing version strings of node and its dependencies.
console.log(process.versions);
Will print something like:
{ http_parser: '1.0',
  
node: '0.10.4',
  
v8: '3.14.5.8',
  
ares: '1.9.0-DEV',
  
uv: '0.10.3',
  
zlib: '1.2.3',
  
modules: '11',
  
openssl: '1.0.1e' }

process.config
An Object containing the JavaScript representation of the configure options that were used to compile the current node executable.

process.memoryUsage()
Returns an object describing the memory usage of the Node process measured in bytes.

process.nextTick(callback)
Once the current event loop turn runs to completion, call the callback function.

console.log('start');
process.nextTick(
function() {
  
console.log('nextTick callback');
});
console.log('scheduled');

process.uptime()

Number of seconds Node has been running.

To know more details about all the topics in Node JS, Please visit Best Node JS Training in Chennai.

Comments

  1. I am really enjoying reading your well written articles. I think you spend numerous effort and time updating your blog. AWS Training in chennai

    ReplyDelete

Post a Comment

Popular posts from this blog

Node JS Introduction

Node js Introduction: 1.      Node.js is a server-side platform built on Google Chrome's JavaScript Engine (V8 Engine). 2.      cross-platform runtime environment used for development of server-side web applications. 3.      Node.js applications are written in JavaScript and can be run on a wide variety of operating systems. 4.      Node.js is based on an event-driven. 5.      Non-blocking Input/Output. 6.      Node.js also thousands of JavaScript modules which simplifies the development of web applications called as NPM. 7.      Single thread process. Reasons to choose Node js? 1.      Node uses the V8 JavaScript Runtime engine , the one which is used by Google Chrome. 2.      Asynchronous event driven IO helps concurrent request handling .This feature basically means that if ...

Node JS Module Types and Tutorials

Node Module Types Node.js modules are generally categorized by 3 Types. 1.      Core Modules 2.      Local Modules 3.      Third Party Modules Node.js Core Modules: Node.js is a light weight framework. The core modules include bare minimum functionalities of Node.js. These core modules are load automatically when Node.js process starts. However, you need to import the core module first in order to use it in your application. Loading Core Modules: In order to use Node.js core or NPM modules, you first need to import it using require() function as shown below. var module = require('module_name'); As per above syntax, specify the module name in the require() function. The require() function will return an object, function, property or any other JavaScript type, depending on what the specified module returns. Node.js Local Module: Local modules are modules created locally in your Node.j...