QA
Beginner LevelNode JS

What is the Node.js global object and what built-in properties exist (process, __dirname, __filename, globalThis)?

❓ Interview Prompt: Explain global object, globalThis, process object, __dirname, __filename, import.meta.url in ESM, and global variables.

In Node.js, 'global' (or standard 'globalThis') is the root global namespace object (equivalent to 'window' in browsers):

Key Built-in Globals:

•
'process': Provides information and control over the current Node.js process.
•
'__dirname' & '__filename': Absolute directory and file path in CommonJS.
•
'import.meta.url': File URL in ES Modules (use 'fileURLToPath(import.meta.url)' for paths).

Code Implementation Example

javascript
import { fileURLToPath } from 'node:url';
import path from 'node:path';

// ESM equivalent of __dirname and __filename
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

console.log('Process PID:', process.pid);
console.log('Node Version:', process.version);
console.log('Current Dir:', __dirname);
💡Accessing Node.js process metrics and file paths in ESM.
🎯 Interview Key Takeaway

Use process for process metadata, and import.meta.url in ES Modules to resolve absolute directory paths.

#global#process#__dirname#import.meta#Beginner
Back to all Node JS Questions→

More Node JS Interview Questions

Beginner LevelNode JS

How does the Node.js Event Loop work? Explain the 6 execution phases (Timers, Pending, Poll, Check, Close).

Beginner LevelNode JS

What does 'Single-Threaded, Event-Driven, Non-Blocking I/O' mean in Node.js?

Beginner LevelNode JS

What is the difference between process.nextTick() and setImmediate()?