Beginner Fundamentals

The process Object

The global process object gives you information about and control over the current Node.js program. You can read command line arguments, exit the program, and more.

Command line arguments

process.argv is an array of arguments. The first two are the Node path and the script path; your arguments come after.

// Run: node app.js hello world
console.log(process.argv);
// [ '/path/to/node', '/path/to/app.js', 'hello', 'world' ]

const args = process.argv.slice(2);
console.log(args); // [ 'hello', 'world' ]

Exiting the program

process.exit stops the program. Use code 0 for success and a non zero code for an error.

if (process.argv.length < 3) {
  console.error("Missing argument");
  process.exit(1);
}

Current working directory

console.log(process.cwd()); // the folder where you ran node

Other useful properties

  • process.platform returns the operating system.
  • process.version returns the Node.js version.
  • process.pid returns the process id.

Summary

The process object connects your program to the outside world: arguments in, exit codes out, plus details about the environment.