Global objects are available in every Node.js file without importing.
Common globals
console—log,error,table,time/timeEndsetTimeout,setInterval,setImmediateglobalThis— the global object (likewindowin browsers)fetch,URL,Buffer,structuredClone
The process object
process.argv— command-line arguments (an array)process.env— environment variables (process.env.PORT)process.cwd()— the folder you ran the command fromprocess.exit(1)— stop the program (non-zero = error)process.platform—win32,linux,darwinprocess.memoryUsage()
File location
- In CommonJS:
__dirname(folder of the current file) and__filename - In ES modules:
import.meta.dirnameandimport.meta.filename(newer Node versions), orimport.meta.url
Use these to build paths relative to your file, not to where the command was run.
Example
Example — A tiny command-line greeter
JavaScript
// greet.js (run: node greet.js Asha 3)
const [, , name = 'friend', times = '1'] = process.argv;
for (let i = 0; i < Number(times); i++) {
console.log(`Hello, ${name}!`);
}
console.log('Running on:', process.platform);
console.log('Port from env:', process.env.PORT ?? 'not set');
if (!process.argv[2]) {
console.error('Tip: pass your name, e.g. node greet.js Asha');
process.exit(1);
}
node greet.js Asha 3 prints "Hello, Asha!" three times, then the platform and port. process.argv[0] is node and [1] is the script path, so user arguments start at index 2.