process is a global object, always available with no require() needed, that represents the currently running Node program itself — information about it, and ways to control it.
process.argv is an array of everything typed on the command line:
node app.js hello worldconsole.log(process.argv)
// ['/path/to/node', '/path/to/app.js', 'hello', 'world']The first two entries are always the path to Node itself and the path to the script — actual arguments start at index 2:
const args = process.argv.slice(2)
console.log(args) // ['hello', 'world']process.env is an object holding environment variables — values set outside the program, by the operating system or whoever launches it, rather than hardcoded in the source:
console.log(process.env.HOME) // e.g. /home/priya
console.log(process.env.NODE_ENV) // e.g. 'production', or undefinedPORT=4000 node app.jsconst port = process.env.PORT || 3000
console.log(`Using port ${port}`)Database passwords, API keys, and similar secrets should never be written directly into a source file — anyone who can see the code (a teammate, a public repository, an AI code review tool) sees the secret too. Environment variables solve this: the secret lives outside the code, and the code just reads whatever process.env happens to hold at runtime.
const apiKey = process.env.API_KEY
if (!apiKey) {
throw new Error('API_KEY environment variable is required')
}Never hardcode a secret
A hardcoded secret that gets committed to version control is extremely hard to fully remove — even deleting it later, it stays in the project's history forever unless that history is rewritten. Reading it from process.env from the start avoids the problem entirely.
process.exit(code) ends the program immediately. 0 means success; any other number signals an error to whatever launched the script:
if (!apiKey) {
console.error('Missing API_KEY')
process.exit(1)
}