What are Global Objects in Node.js?
Learn what global objects are in Node.js — process, console, Buffer and timers — how they differ from browser globals, and why __dirname is not truly global.
Expected Interview Answer
Global objects in Node.js are built-in objects and values available in every module without requiring an import, such as global, process, console, Buffer, __dirname, __filename, and the timer functions.
Unlike the browser where window is the global, Node.js uses global (and globalThis in modern versions) as the top-level scope. Some 'globals' like __dirname, __filename, require, module, and exports are not truly global — they are injected per-module wrapper arguments — while process, console, Buffer, setTimeout, and URL are genuinely global. Overusing global state is discouraged because it couples modules and makes testing harder.
- Access common utilities without imports
- process exposes environment, arguments and runtime info
- console provides logging everywhere
- Buffer handles binary data natively
- Timers (setTimeout, setInterval) are always available
AI Mentor Explanation
Global objects are like the fixed fittings of a cricket ground that every match uses without bringing them — the pitch, the boundary rope, the sight screens and the scoreboard are simply there. No team packs a boundary rope in its kit bag; it is provided by the venue for all. In Node.js, process, console and Buffer are those permanent ground fixtures available to every innings of code.
Step-by-Step Explanation
Step 1
Know the true global scope
In Node.js the top-level object is global (and globalThis); it is not window as in browsers.
Step 2
Identify genuine globals
process, console, Buffer, URL, setTimeout, setInterval and queueMicrotask are available in every module without import.
Step 3
Recognize module-scoped pseudo-globals
__dirname, __filename, require, module and exports look global but are injected per file by the module wrapper.
Step 4
Use process for runtime info
Read process.env, process.argv, and process.platform to inspect environment and inputs.
Step 5
Avoid polluting global
Attach shared state to modules or dependency injection rather than assigning to global to keep code testable.
What Interviewer Expects
- Difference between global and browser window
- Naming several true globals correctly
- Awareness that __dirname/require are module-scoped, not global
- Understanding of process and its common properties
- Why overusing global state is bad practice
Common Mistakes
- Claiming window exists in Node.js
- Thinking __dirname and require are truly global
- Confusing global with globalThis behavior
- Assigning application state onto global carelessly
Best Answer (HR Friendly)
“Global objects in Node.js are ready-made helpers you can use anywhere without importing them, like console for logging and process for reading environment settings. They save you from re-loading common tools in every file, though it is best not to overuse global storage.”
Code Example
// console, process and Buffer are all globally available
console.log('Running on:', process.platform);
console.log('Args:', process.argv.slice(2));
console.log('API key set:', Boolean(process.env.API_KEY));
// Buffer for binary data — no import needed
const buf = Buffer.from('hello', 'utf8');
console.log(buf.toString('hex'));
// __dirname looks global but is injected per module
console.log('This file lives in:', __dirname);
// setTimeout is a genuine global timer function
setTimeout(() => console.log('one second later'), 1000);Follow-up Questions
- What is the difference between global and globalThis in Node.js?
- Why are __dirname and require not truly global?
- What common properties does the process object expose?
- How does the Node.js module wrapper function work?
- Why is relying on global state considered an anti-pattern?
MCQ Practice
1. Which of these is NOT a truly global object in Node.js?
__dirname is injected into each module by the module wrapper, so it is module-scoped rather than a real global.
2. What is the top-level global object in Node.js?
Node.js uses global (and globalThis) as the top-level scope; window and document belong to the browser.
3. Which global would you use to read environment variables?
process.env holds the environment variables of the running Node.js process.
Flash Cards
What is the Node.js global scope object? — global (and globalThis), unlike the browser's window.
Name three genuine Node.js globals. — process, console, and Buffer (plus timers like setTimeout).
Is __dirname a true global? — No — it is injected per module by the module wrapper function.
How do you read environment variables? — Through process.env, e.g. process.env.API_KEY.