Core Globals (process, __dirname)

Node.js provides a set of global objects available in every file without importing them — most importantly process (information and control over the running program) and __dirname and __filename (the absolute path of the current directory and file).

Learn Core Globals (process, __dirname) in our free Node.js course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a…

Part of the free Node.js course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

By the end of this lesson you'll read command-line arguments and environment variables from process , build reliable file paths with __dirname , and recognise the other always-available globals like console , timers, and Buffer .

What You'll Learn in This Lesson

1️⃣ The process Object

The process global is your program's connection to the world it runs in. It carries the arguments you were started with, the environment around you, facts about the Node runtime, and the controls to stop the program. The four pieces you'll reach for constantly:

Now run that file and pass it two arguments. Watch how the words you type land in process.argv starting at index 2:

2️⃣ __dirname and __filename

These two globals tell you where the current file lives on disk , as absolute paths. __dirname is the folder, __filename is the full path including the file name. They matter because Node resolves relative paths from the current working directory (where you ran node ), which can change — but __dirname never does. To open a file that sits next to your script, build the path from __dirname so it works wherever the program is launched.

3️⃣ The Other Globals You'll Meet

A handful of other globals are always on hand, no import required:

Your turn. Fill in the two blanks marked ___ so the program reads a name from the command line and greets it. Follow the 👉 hints, then run it both with and without an argument.

No blanks this time — just a brief and an outline. Write it yourself, run it, and check your output against the example in the comments. It pulls together process.argv , process.version , process.platform , and __dirname in one tiny program.

📋 Quick Reference — Node Globals

Practice quiz

Which global must be imported with require before you can use it?

  • process
  • console
  • None of these — they are all global
  • __dirname

Answer: None of these — they are all global. process, console, and __dirname are all globals available with no import in Node.

In process.argv, at what index do YOUR command-line arguments begin?

  • At index 2
  • At index 0
  • At index 1
  • At index 3

Answer: At index 2. Index 0 is the node path and index 1 is the script path, so your real args start at index 2.

What does process.env.NOPE return when that variable is not set?

  • An empty string
  • It throws an error
  • undefined
  • null

Answer: undefined. Reading a missing environment variable returns undefined rather than throwing.

What does __dirname hold?

  • The absolute path of the folder the current file lives in
  • The current working directory
  • The file's name only
  • The path to the node binary

Answer: The absolute path of the folder the current file lives in. __dirname is the absolute path of the folder containing the current file (CommonJS).

Which global is used to work with raw binary data (bytes) in Node?

  • Blob
  • ArrayList
  • Buffer
  • ByteStream

Answer: Buffer. Buffer is the global for handling raw bytes; Buffer.from('Hi') yields <Buffer 48 69>.

What does process.exit(0) signal to the operating system?

  • A warning
  • An error
  • Success
  • A restart

Answer: Success. Exit code 0 conventionally means the program finished successfully.

Which property gives the Node.js version string like v20.11.0?

  • process.node
  • process.version
  • process.platform
  • process.pid

Answer: process.version. process.version returns the running Node.js version string.

Where is __dirname NOT available?

  • In CommonJS files
  • In scripts run with node
  • In ES modules (.mjs or type:module)
  • In files using require

Answer: In ES modules (.mjs or type:module). ES modules have no __dirname; you derive it from import.meta.url instead.

What does globalThis refer to?

  • The current module's exports
  • The one true global object across environments
  • The process object
  • The console object

Answer: The one true global object across environments. globalThis is the standard reference to the global object in any JS environment.

Where does console.error write its output?

  • To a log file
  • To stdout
  • It does nothing
  • To stderr

Answer: To stderr. console.error writes to standard error (stderr), separate from stdout.