CodingNic

Node.js Fundamentals

Global Objects

Node.js Fundamentals 8 min read

Global Objects

Objectives

By the end of this lesson, you should be able to:

  • List Node’s most commonly used global objects
  • Use __dirname and __filename to work with the current file’s location
  • Explain what process and global each represent

💡 Why this matters: These globals are available in every Node.js file without any require or import, they show up constantly in real code, especially process, 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

javascript
console.log(__dirname);
console.log(__filename);
text
/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: __dirname and __filename are only available in CommonJS files. ES Modules (covered in Lesson 5) don’t have them directly, they use import.meta.url instead.

process, global, console, and Buffer

javascript
console.log(typeof process);
console.log(typeof global);
console.log(typeof console);
console.log(typeof Buffer);
text
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’s window, 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

javascript
console.log("Node version:", process.version);
console.log("Platform:", process.platform);
console.log("PID:", typeof process.pid);
console.log("Working directory:", typeof process.cwd());
text
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

  1. Write a file that logs __dirname and __filename, and confirm both print an absolute path.
  2. Log process.platform and process.version on your own machine, and note the results.
  3. Explain, in your own words, why __dirname and __filename are useful for building file paths reliably, compared to typing a path by hand.

Recap

  • __dirname and __filename (CommonJS only) give the current file’s directory and full path.
  • process, global, console, and Buffer are available everywhere without any import.
  • process specifically 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.