Global Objects
Objectives
By the end of this lesson, you should be able to:
- List Node’s most commonly used global objects
- Use
__dirnameand__filenameto work with the current file’s location - Explain what
processandglobaleach represent
💡 Why this matters: These globals are available in every Node.js file without any
requireorimport, they show up constantly in real code, especiallyprocess, used again and again throughout this course.
⚠️ A note on verification: every snippet and output in this lesson was actually run with Node.js.
__dirname and __filename
console.log(__dirname);
console.log(__filename);
/tmp/node103
/tmp/node103/globals.js
__dirname is the absolute path to the directory containing the current file, __filename is the absolute path to the file itself. These are extremely useful for building reliable file paths (with the path module, covered in the next module) that work regardless of where a program is run from.
⚠️ Note:
__dirnameand__filenameare only available in CommonJS files. ES Modules (covered in Lesson 5) don’t have them directly, they useimport.meta.urlinstead.
process, global, console, and Buffer
console.log(typeof process);
console.log(typeof global);
console.log(typeof console);
console.log(typeof Buffer);
object
object
object
function
process, an object representing the currently running Node.js program itself, environment variables (Lesson 6), command-line arguments (Module 1), and more.global, Node’s equivalent of the browser’swindow, an object holding globally accessible values, rarely used directly in modern code, but it’s why globals work without importing anything.console, the familiar logging object,console.log,console.error,console.trace(Lesson 7), and more.Buffer, a Node-specific way of working with raw binary data, used when handling things like file contents or network data that aren’t plain text.
A Closer Look at process
console.log("Node version:", process.version);
console.log("Platform:", process.platform);
console.log("PID:", typeof process.pid);
console.log("Working directory:", typeof process.cwd());
Node version: v22.22.3
Platform: linux
PID: number
Working directory: string
process.version reports the running Node.js version, process.platform reports the operating system, process.pid is the running program’s process ID, and process.cwd() returns the current working directory as a string. process shows up constantly, process.argv (Module 1) for command-line arguments, process.env (Lesson 6) for environment variables, and process.exit() to end a program manually.
Try It
- Write a file that logs
__dirnameand__filename, and confirm both print an absolute path. - Log
process.platformandprocess.versionon your own machine, and note the results. - Explain, in your own words, why
__dirnameand__filenameare useful for building file paths reliably, compared to typing a path by hand.
Recap
__dirnameand__filename(CommonJS only) give the current file’s directory and full path.process,global,console, andBufferare available everywhere without any import.processspecifically is used constantly throughout real Node.js code, for environment info, arguments, and more.
Next lesson: package.json, npm, and npx, managing a real Node.js project.