Loading

Quipoin Menu

Learn • Practice • Grow

node-js / Global Objects
tutorial

Global Objects

Imagine you're in a room and you need to know where you are, what time it is, and who you are. In Node.js, there are special objects available everywhere – you don't need to import them. These are called **global objects**.

What are Global Objects?

Global objects are objects that are available in all modules without requiring them. They're like the air around you – always there when you need them.

Think of global objects as the room you're sitting in – you don't need to ask for permission to know where the walls are, they're just there.

Important Global Objects

Global ObjectDescription
`__dirname`The directory name of the current module.
`__filename`The filename of the current module (with full path).
`process`Information about the current Node.js process.
`module`Information about the current module.
`exports`A reference to `module.exports` – used to export values.
`require()`Function to import modules.
`console`For printing to terminal (we've been using this!).

__dirname and __filename

These give you information about where your current file is located.
<!-- Create a file called path-demo.js -->
console.log('__dirname:', __dirname);
console.log('__filename:', __filename);

Run it:
node path-demo.js

Output:
__dirname: /Users/yourname/projects/node-demo
__filename: /Users/yourname/projects/node-demo/path-demo.js

The process Object

The `process` object is a global that provides information about the current Node.js process. It's incredibly useful for getting environment info, command-line arguments, and controlling the process.
<!-- process-info.js -->
console.log('Node.js version:', process.version);
console.log('Process ID:', process.pid);
console.log('Current working directory:', process.cwd());
console.log('Memory usage:', process.memoryUsage());

<!-- Command line arguments -->
console.log('Arguments:', process.argv);

Run with arguments:
node process-info.js hello world 123

process.argv – Command Line Arguments

`process.argv` is an array containing command-line arguments. The first element is the path to Node.js, the second is the path to your script, and the rest are your arguments.
<!-- greet.js -->
const args = process.argv.slice(2); <!-- Get user arguments -->
const name = args[0] || 'World';
console.log(`Hello, ${name}!`);

Run:
node greet.js Alice
Output: Hello, Alice!

Environment Variables with process.env

`process.env` contains the user environment. This is where you store configuration like API keys, database passwords, etc.
console.log('Home directory:', process.env.HOME);
console.log('PATH:', process.env.PATH);

<!-- Set custom environment variable: NODE_ENV=development node env-test.js -->
console.log('Node environment:', process.env.NODE_ENV || 'development');

process.exit() – Exiting the Process

You can exit the Node.js process with `process.exit()`. You can pass an exit code (0 for success, non-zero for error).
if (errorHappened) {
  console.error('Something went wrong!');
  process.exit(1); <!-- Exit with error code -->
}

The console Object

We've used `console.log()`, but there are other useful methods:
console.error('This is an error message'); <!-- For errors -->
console.warn('This is a warning'); <!-- For warnings -->
console.table({ name: 'John', age: 30 }); <!-- Displays as table -->
console.time('label');
<!-- some code -->
console.timeEnd('label'); <!-- Shows time taken -->

Two Minute Drill

  • Global objects are available everywhere without `require()`.
  • `__dirname` and `__filename` give you file path information.
  • `process` object provides control over the Node.js process.
  • `process.argv` gets command-line arguments.
  • `process.env` accesses environment variables.
  • `console` has many useful methods beyond `log()`.

Need more clarification?

Drop us an email at career@quipoinfotech.com