Working with Globals
1. Using __dirname and __filename
| Variable | Value | Module |
|---|---|---|
__dirname | Absolute dir path of current file | CommonJS only |
__filename | Absolute file path | CommonJS only |
import.meta.dirname | Equivalent in ES modules v20.11+ | ESM |
import.meta.filename | ESM equivalent v20.11+ | ESM |
2. Understanding global Object
| Property | Description |
|---|---|
global | Top-level scope object in Node (like window) |
global.foo = 1 | Assign cross-module global (avoid) |
3. Using globalThis
Note:
globalThis is the cross-environment standard. Prefer it over global for portable code (works in browser, Worker, Node).| Environment | Resolves To |
|---|---|
| Node | global |
| Browser | window |
| Worker | self |
4. Using console Methods
| Method | Output Stream | Use |
|---|---|---|
console.log | stdout | General logs |
console.error | stderr | Errors |
console.warn | stderr | Warnings |
console.info | stdout | Info |
console.debug | stdout | Debug (when enabled) |
console.table(arr) | stdout | Tabular output |
console.time/timeEnd | stdout | Measure duration |
console.trace | stderr | Stack trace |
console.dir(obj, opts) | stdout | Inspect with options |
console.group/groupEnd | stdout | Indented blocks |
console.assert(cond, msg) | stderr | Print if cond falsy |
5. Using Buffer Global
| Constructor | Purpose |
|---|---|
Buffer.from(str, "utf8") | From string |
Buffer.alloc(size) | Zero-filled bytes |
Buffer.allocUnsafe(size) | Faster, uninitialized memory |
6. Using process Global
| Property | Description |
|---|---|
process.argv | CLI arguments |
process.env | Environment variables |
process.cwd() | Current working dir |
process.platform | linux / darwin / win32 |
process.exit(code) | Exit with code |
7. Using setTimeout/setInterval Globals
| API | Returns | Cancel With |
|---|---|---|
setTimeout(fn, ms) | Timeout object | clearTimeout |
setInterval(fn, ms) | Timeout object | clearInterval |
setImmediate(fn) | Immediate | clearImmediate |
queueMicrotask(fn) | void | n/a |
8. Understanding module Object
| Property | Description |
|---|---|
module.exports | What this module exports |
module.id | Identifier (filename) |
module.filename | Absolute path |
module.children | Modules required by this one |
module.paths | Lookup paths |
9. Using require Function
| Form | Resolves To |
|---|---|
require("fs") | Built-in module |
require("./util") | Local file |
require("lodash") | node_modules package |
require.resolve("x") | Get resolved path |
require.cache | Loaded modules cache |
10. Using exports and module.exports
Example: Two export patterns
// Single export
module.exports = function greet(name) { return "Hi " + name; };
// Multiple named exports
exports.add = (a, b) => a + b;
exports.sub = (a, b) => a - b;
| Form | When |
|---|---|
module.exports = X | Replacing entire export |
exports.foo = X | Adding to default export object |
Warning: Reassigning
exports = ... breaks the binding. Always use module.exports = ....11. Avoiding Global Variable Pollution
| Anti-pattern | Fix |
|---|---|
global.db = ... | Pass via DI / module export |
Implicit globals (no const) | Use "use strict" / ESM (strict by default) |
| Monkey-patching globals | Wrap in dedicated modules |