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' codeprocess.exit(0); //To exit with a 'success' codeNote : 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 inputnode server.js --testme happySample output0 : C:\Program Files\nodejs\node.exe1 : E:\MyAutoSyncBox\Dropbox\1.My Study Sync Box\StudyBoxNotes\08_Javascript\04_NODEJS\02_prac\server.js2 : --testme3 : happyprocess.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 :So all these event stream has listener methods. Note : console.log() goes to stdout, console.error() goes to stderrprocess.stderr : returns a stream connected to stderr/ writable fileprocess.stdin : returns a stream connected to stdin/ readable fileprocess.stdout : returns a stream connected to stdout/ writable file