Hiprup

What are global objects in Node.js?

Global objects are objects and variables available everywhere in a Node.js application without needing to import them. They're part of Node's runtime environment, similar to how window works in the browser.

Common global objects:

  • global — the global namespace itself (equivalent to window in browsers).

  • process — info about the current Node process (env variables, arguments, exit handling).

  • console — logging (console.log, console.error, etc.).

  • Buffer — handles binary data.

  • __dirname — absolute path of the current module's directory.

  • __filename — absolute path of the current file.

  • setTimeout / setInterval / setImmediate — timers.

  • clearTimeout / clearInterval / clearImmediate — cancel timers.

  • require (CommonJS) / module / exports — module system.

  • queueMicrotask — schedule microtasks.

// Common global objects
console.log(__dirname);        // /Users/app/src
console.log(__filename);       // /Users/app/src/index.js
console.log(process.version);  // v20.11.0
console.log(process.platform); // darwin
console.log(process.pid);      // 12345

// Buffer is global
const buf = Buffer.from('Hello');
console.log(buf.toString('hex')); // 48656c6c6f

This shows several global objects in action. __dirname and __filename give the current file's location. The process object exposes the Node.js version, platform, and process ID.

Buffer is available globally for binary data manipulation without requiring an import.

Mention that __dirname and __filename look global but are actually module-scoped. This detail shows you understand how the Node.js module wrapper works.

What are global objects in Node.js? | Hiprup