Skip to main content

Menu

System onlineAI Tutorials / 2026

AI Tutorials
Knowledge search

Recent · Popular · Tutorials

  • dart3 tutorials
  • java1 tutorial
  • Javascript3 tutorials
  • Node.js Introduction2 tutorials
  • dartdart · General · In depth
  • JavascriptJavascript · Advanced · In depth
  • JavascriptJavascript · Advanced · Mid
  • JavascriptJavascript · Advanced · Short

Find a tutorial

Tell the assistant what you want to learn. It suggests published chapters.

For example: “I want to learn Rust ownership from the start.” The conversation is kept only in this tab.

0 / 2,000 characters

Enter to send, Shift+Enter for a new line. Do not include personal information.

Chapter 10

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
Level: GeneralLength: Mid2 min read

Read at another depth

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:

Code example in javascript
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:

Code example in javascript
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.

PropertyWhat it gives youExample 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:

Code example in javascript
// args.js
process.argv.forEach((word, index) => {
  console.log(index, word);
});

Run node args.js hello world, and you get:

Code example in text
0 /usr/local/bin/node
1 /home/lee/args.js
2 hello
3 world

The 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:

Code example in javascript
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:

Code example in javascript
// 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?

Report an issue

Chapter: Global Objects and the Process

0 / 2,000 characters

Plain text, for example: the second code sample does not compile. Do not include personal information.

You can't do that