Global Objects and the Process
Every JavaScript environment has certain things you can use anywhere, without importing or installing anything. In the browser, these include window and document, which represent the web page. Node.js is JavaScript outside the browser, so it has its own set of built-in helpers. These are called global objects — values and functions that are available everywhere in your program.
In this chapter, we meet the most useful ones: console, __dirname, process, and global. Understanding them will let you print messages, find out where your files live, and learn how your program is being run.
console: Printing Messages
console is your way to send text to the terminal. You have likely already used it:
console.log('A normal message');
console.error('Something went wrong');
console.warn('A warning');Run this with node, and all three lines appear in your terminal. Why three different commands? Because they signal different levels of importance. Many tools treat console.error and console.warn specially — for example, error logs are often separated from normal output so problems stand out.
__dirname: Where Am I?
__dirname (pronounced "dir name") is a string containing the full path to the folder your script lives in. It is not available in the browser — it is a Node.js-only feature.
Imagine you have a file at /home/lee/projects/notes.js. Inside that file, this line:
console.log(__dirname);prints /home/lee/projects.
Why is this useful? Because when your script reads another file, you usually want that file relative to your script, not relative to wherever you happened to run the command from. __dirname gives you a reliable anchor point.
process: Talking to the World Around You
process is one of the most important globals in Node.js. It is an object that represents the running program itself. Through it, you can ask questions like "Who launched me?" and "With what settings?"
Three process properties come up constantly.
| Property | What it gives you | Example value |
|---|---|---|
| `process.argv` | The words typed on the command line | `['node', 'notes.js', 'hello']` |
| `process.env` | Environment variables — named settings from the operating system | `{ HOME: '/home/lee' }` |
| `process.exit(1)` | Ends the program immediately; the number is a code telling other tools if it succeeded (0) or failed (anything else) | `process.exit(1)` |
Let's see process.argv in action:
// args.js
process.argv.forEach((word, index) => {
console.log(index, word);
});Run node args.js hello world, and you get:
0 /usr/local/bin/node
1 /home/lee/args.js
2 hello
3 worldThe first two entries are always the node program itself and your script's path. Anything the user typed after that starts at position 2. This is how small Node.js tools accept commands like node backup.js --dry-run.
Environment variables in process.env are the usual way to pass secrets (like passwords) into a program without writing them into your code. You will meet them again when you configure real applications.
global: The Root Object
global is the overall container for Node.js globals — roughly equivalent to the browser's window. You rarely use it directly, but it explains why console and process are available without any import: they hang off global. A helpful check if you are ever unsure which environment you are in:
if (typeof global !== 'undefined') {
console.log('Running in Node.js');
} else {
console.log('Running in a browser');
}Putting It Together
A tiny script that uses several globals at once:
// about.js
console.log('Script folder:', __dirname);
console.log('Program name:', process.argv[1]);
console.log('Home folder:', process.env.HOME);Exercise
You run node greet.js Ada. What is process.argv[2]?
Show answer
The string 'Ada'. Position 0 is the node program, position 1 is greet.js, so the first user-typed word is at position 2.
Exercise
Why is __dirname more reliable than a hard-coded folder path in your script?
Show answer
A hard-coded path breaks if the project moves or another person clones it to a different folder. __dirname always points to wherever the script actually lives.
Spotted a mistake, or code that does not work?