SyntaxStudy
Sign Up
Node.js Built-in Node.js Modules
Node.js Beginner 8 min read

Built-in Node.js Modules

Node.js ships with many built-in modules that cover common tasks like file system access, networking, path manipulation, and cryptography — no installation required.

Key built-ins: fs, path, os, http, crypto, events, util.

Example
const path   = require('path');
const os     = require('os');
const crypto = require('crypto');
const util   = require('util');
const events = require('events');

// path — cross-platform file paths
console.log(path.basename('/usr/local/bin/node'));  // 'node'
console.log(path.extname('index.html'));            // '.html'
console.log(path.join('src', 'utils', 'helper.js'));

// os — system information
console.log(os.hostname());     // machine name
console.log(os.cpus().length);  // number of CPU cores
console.log(os.freemem());      // free RAM in bytes

// crypto — hashing
const hash = crypto.createHash('sha256')
    .update('my secret password')
    .digest('hex');
console.log(hash);

// events — EventEmitter
const emitter = new events.EventEmitter();
emitter.on('data', (msg) => console.log('Received:', msg));
emitter.emit('data', 'Hello from EventEmitter');

// util — promisify a callback function
const fs = require('fs');
const readFileAsync = util.promisify(fs.readFile);