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:
- • process.argv — an array of command-line arguments. Index 0 is node, index 1 is your script, and your real arguments start at index 2 .
- • process.env — an object of environment variables (all values are strings). A variable that isn't set reads as undefined , not an error.
- • process.version / process.platform / process.pid — read-only facts: the Node version, the OS, and this process's id number.
- • process.exit(code) — stops the program right now. 0 means success; any other number signals an error.
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:
- • console — printing and logging ( console.log , console.error , and friends).
- • setTimeout / setInterval — run code later, once or repeatedly (cancel with clearTimeout / clearInterval ).
- • Buffer — work with raw binary data (bytes), used heavily by files and networking.
- • globalThis — the single, standard global object that works the same in Node and the browser.
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.
Mini-Challenge: A System Info Reporter
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.
Common Errors (and the fix)
- Reading the wrong argv index — process.argv[0] is the node binary and process.argv[1] is your script. Your real arguments start at [2] . Use process.argv.slice(2) to skip the first two cleanly.
- "__dirname is not defined" — You're in an ES module ( .mjs or "type": "module" ), where __dirname doesn't exist. Derive it with dirname(fileURLToPath(import.meta.url)) , or rename the file to .cjs .
- Treating a missing env var as an error — An unset process.env.SOMETHING is undefined , not a crash. Guard with a default: const port = process.env.PORT || 3000; .
- Mutating process.env expecting it to persist — You can set process.env.X = "..." , but it only lives for this process; it does not change your real shell environment, and the value is always coerced to a string.
- Calling process.exit() too early — process.exit() stops immediately and can cut off pending async output (logs, file writes). Let the program finish naturally where you can, instead of exiting in the middle of async work.
Pro Tips
- 💡 Slice your args: reach for process.argv.slice(2) right away so you never juggle the first two fixed entries by hand.
- 💡 Build paths, don't concatenate them: use path.join(__dirname, "file.txt") instead of string addition so it works on every OS.
- 💡 Default your env vars: a pattern like const env = process.env.NODE_ENV || "development"; keeps things running when a variable is missing.
📋 Quick Reference — Node Globals
Goal
Global
Command-line arguments
process.argv
Read an environment variable
process.env.NAME
Node version
process.version
Current directory of this file
__dirname
Full path of this file
__filename
Build a path next to the script
path.join(__dirname, "x")
Exit the program (success)
process.exit(0)
Frequently Asked Questions
🎉 Lesson Complete!
- ✅ Globals like process and console are available in every file with no import
- ✅ process.argv holds command-line args; your real ones start at index 2
- ✅ process.env reads environment variables; missing ones are undefined
- ✅ process.version , process.platform , and process.exit() inspect and control the runtime
- ✅ __dirname and __filename give reliable absolute paths (CommonJS only)
- ✅ console , timers, Buffer , and globalThis are the other always-on globals
- ✅ Next lesson: The fs Module — read and write files from your Node programs
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.
Continue this course
- Previous: The Event Loop
- Next: The fs Module