Process

Docs

It provides information about and control over the current Node.js process

Events

  • The process object is an instance of EventEmitter.

    So events emitted by it can be listened by ".on()" method

    const process = require('process'); // process also available in global object(.
    )
    process.on('exit', (code) => {
    console.log('Process exit event with code: ', code);
    });

    ...more in docs

Methods

  • process.abort() : method causes the Node.js process to exit immediately Note : This feature is not available in Worker threads.

  • process.cwd() : the current working directory of the Node.js process.

process.exit()

  • process.exit() :to terminate the process synchronously with an exit status of code (even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr)

    process.exit(1); //To exit with a 'failure' code
    process.exit(0); //To exit with a 'success' code

    Note : all the 'exit' event listeners are called first then process terminated.

  • Normally Node.js process will exit on its own if there is no additional work pending in the event loop

  • If it is necessary to terminate the Node.js process due to an error condition, throwing an uncaught error and allowing the process to terminate accordingly is safer than calling process.exit().

  • It is safer to use 'process.exitCode = 1 or specific;' instead process.exit() if async operations still exists : It will allow the process to exit naturally by avoiding scheduling any additional work for the event loop exitCodes

Properties

  • process.argv : array containing the command line arguments

    if (process.argv && process.argv.length) {
    process.argv.forEach((value, index) => {
    console.log(`${index} : ${value}`);
    })
    }
    Sample input
    node server.js --testme happy
    Sample output
    0 : C:\Program Files\nodejs\node.exe
    1 : E:\MyAutoSyncBox\Dropbox\
    1.My Study Sync Box\StudyBoxNotes\
    08_Javascript\04_NODEJS\02_prac\server.js
    2 : --testme
    3 : happy
  • process.env : object containing the user environment

  • process.title : current process title (get/ set)

  • process.pid : process identifier (PID)

  • process.version : Node.js version string // process.versions = detailed dependent libs version

Connected streams

  • Process has following properties :
    process.stderr : returns a stream connected to stderr/ writable file
    process.stdin : returns a stream connected to stdin/ readable file
    process.stdout : returns a stream connected to stdout/ writable file
    So all these event stream has listener methods. Note : console.log() goes to stdout, console.error() goes to stderr
process.stdin.setEncoding('utf8');
process.stdin.on('readable', () => {
let chunk;
// Use a loop to make sure we read all available data.
while ((chunk = process.stdin.read()) !== null) {
process.stdout.write(`data: ${chunk}`);
}
});
process.stdin.on('end', () => {
process.stdout.write('end');
});
Custom example :
process.stdin.setEncoding('utf8');
process.stdin.on('readable', () => {
let chunk = process.stdin.read(); //one time read
while (chunk !== null) { //check gain this is to read multi line
if (chunk && chunk.trim() == "closeme") {
process.exit();
}
process.stdout.write(`data: ${chunk}`);
//chunk = null; to break loop
chunk = process.stdin.read(); //trigger's next read event too ! ?
}
//----------
})
process.on('exit', () => {
console.log('process exiting');
})