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 exportmodule.exports = function greet(name) { return "Hi " + name; };// Multiple named exportsexports.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 = ....