Appearance
Basic Modules
Since Node.js operates as a server-side JavaScript environment, its programs differ significantly from those running in browsers, particularly in the absence of security restrictions typical in browsers. Additionally, server programs must handle network requests, read/write files, and manage binary content. Therefore, Node.js comes with built-in modules designed for basic server functionalities, which cannot be executed in a browser environment because their underlying code is implemented in C/C++.
Global Object
In JavaScript, there is only one global object: window
in browsers. In Node.js, this object is named global
. The properties and methods of global
differ from those of window
. You can explore it in the Node.js interactive environment:
javascript
> global.console
Object [console] {
log: [Function: log],
warn: [Function: warn],
dir: [Function: dir],
time: [Function: time],
...
}
Process Object
The process
object represents the current Node.js process and provides many useful details:
javascript
> process === global.process;
true
> process.version;
'v22.3.0'
> process.platform;
'darwin'
> process.arch;
'x64'
> process.cwd(); // Returns the current working directory
'/Users/michael'
> process.chdir('/private/tmp'); // Change the current working directory
undefined
> process.cwd();
'/private/tmp'
JavaScript operates on an event-driven, single-threaded model, and Node.js follows this model as well. Node.js continually executes JavaScript functions that respond to events until there are no more events to respond to, at which point it exits.
To execute code in the next event response, you can use process.nextTick()
:
javascript
// test.js
process.nextTick(function () {
console.log('nextTick callback!');
});
console.log('nextTick was set!');
Running this with node test.js
will produce:
nextTick was set!
nextTick callback!
This indicates that the function passed to process.nextTick()
is not executed immediately but waits until the next event loop iteration.
Node.js processes handle events via the process
object. For example, you can respond to the exit
event with a callback:
javascript
process.on('exit', function (code) {
console.log('about to exit with code: ' + code);
});
Determining the JavaScript Execution Environment
Some JavaScript code can run in both browser and Node.js environments. To determine the execution context, you can check for the existence of global variables:
javascript
if (typeof(window) === 'undefined') {
console.log('node.js');
} else {
console.log('browser');
}
Importing Node Modules
Node.js includes many built-in modules, and you can find information on all available modules in the Node.js online documentation.
For instance, to use the randomInt()
function from the crypto
module, you can import it in two ways:
Method 1: Using the traditional require()
:
javascript
const { randomInt } = require('node:crypto');
const n = randomInt(0, 100); // Random number between 0 and 100
console.log(n);
Method 2: Using the import
keyword for ESM modules:
javascript
import { randomInt } from 'node:crypto';
const n = randomInt(0, 100); // Random number between 0 and 100
console.log(n);
In the following sections, we will introduce commonly used built-in modules in Node.js.