Node.js Globals
Work with process, Buffer, timers, module paths, and other runtime-provided values.
Lesson content
🎯 Goal By the end of this lesson, you'll understand: What are Global Objects? Why do we use them? Common Node.js Global Objects Real-world examples Interview questions What are Global Objects? A Global Object is an object or function that is available everywhere in your Node.js application. You don't need to import it. Simple Definition: Global objects are built into Node.js and can be used directly. Real-Life Example 🏠 Imagine your home. Things like: 💡 Electricity 🚰 Water 🌐 Wi-Fi are available in every room. You don't carry them from one room to another. Node.js Globals work the same way. Example console . log( "Hello Node.js" ); Did we import console ? ❌ No. Because console is a Global Object . Most Common Node.js Globals Global Purpose global Global object console Print messages process Information about the running Node.js process __dirname Current folder path __filename Current file path setTimeout() Run code after a delay setInterval() Run code repeatedly clearTimeout() Stop a timeout clearInterval() Stop an interval Buffer Work with binary data 1. global The global object is the root global object in Node.js. global . message = "Hello" ; console . log( global . message); Output Hello 2. console Used for printing messages. console . log( "Hello" ); console . error( "Error" ); console . warn( "Warning" ); Output Hello Error Warning 3. process The process object provides information about the current Node.js process. console . log( process . platform); Example Output win32 Current Working Directory console . log( process . cwd()); Example Output C:\Projects\NodeApp Environment Variables console . log( process . env . NODE_ENV); Output development Exit the Program process . exit(); Stops the application immediately. 4. __dirname Returns the directory of the current file. console . log( __dirname ); Output C:\Projects\NodeApp 5. __filename Returns the full path of the current file. console . log( __filename ); Output C:\Projects\NodeApp\app.js Difference __dirname │ ▼ C:\Projects\NodeApp __filename │ ▼ C:\Projects\NodeApp\app.js 6. setTimeout() Runs code once after a delay. setTimeout (() => { console . log( "Hello" ); }, 2000 ); Output (After 2 seconds) Hello 7. setInterval() Runs code repeatedly. setInterval (() => { console . log( "Running..." ); }, 1000 ); Output Running... Running... Running... ... Stop an Interval const timer = setInterval (() => { console . log( "Running..." ); }, 1000 ); setTimeout (() => { clearInterval ( timer ); }, 5000 ); The interval stops after 5 seconds. 8. Buffer A Buffer stores binary data. const buffer = Buffer . from( "Hello" ); console . log( buffer ); Output <Buffer 48 65 6c 6c 6f> Convert it back to text. console . log( buffer . toString()); Output Hello Real-Life Example 📁 Suppose you're uploading an image. Image │ ▼ Buffer │ ▼ Save to Disk Node.js uses Buffer to handle binary data like images, videos, and files. Global vs Local Variables const name = "Ajay" ; name is only available in this file. global . name = "Ajay" ; name is available through global.name . ⚠️ In modern Node.js, avoid storing application data on global unless absolutely necessary. CommonJS vs ES Modules One important difference: Feature CommonJS ES Modules __dirname ✅ Available ❌ Not available directly __filename ✅ Available ❌ Not available directly In ES Modules, you create equivalents using import.meta.url . Interview Question What is the output? console . log( __dirname ); console . log( __filename ); Output C:\Projects\NodeApp C:\Projects\NodeApp\app.js (The actual paths depend on where your file is located.) Best Practices ✅ Use process.env for configuration (API keys, database URLs). ✅ Use __dirname when reading files relative to your project. ✅ Use setTimeout() and setInterval() for scheduling tasks. ❌ Avoid adding custom properties to the global object unless there's a clear need. Key Points ✅ Global objects are available without importing them. ✅ console is used for logging. ✅ process gives information about the running application. ✅ __dirname returns the current folder path. ✅ __filename returns the current file path. ✅ Buffer is used for binary data. ✅ setTimeout() runs code once after a delay. ✅ setInterval() runs code repeatedly. 🧠 Easy Formula Node.js Globals console → Print messages process → App & system information __dirname → Current folder __filename → Current file Buffer → Binary data setTimeout → Run once later setInterval → Run repeatedly global → Global object Remember: Node.js Globals are built-in objects and functions that you can use anywhere in your Node.js application without importing them.