Standard JavaScript Library (Node.js): Complete Overview
Node.js is a JavaScript runtime environment built on Google's V8 engine. Unlike browser JavaScript, Node.js offers a powerful built-in standard library that allows you to work with the file system, network, data streams, cryptography, and much more. In this article, we will take a detailed look at the key modules of the Node.js standard library that every developer needs.
1. Module fs (File System)
The fs module provides an API for interacting with the file system. It supports both synchronous and asynchronous operations.
- Reading a file:
const fs = require('fs');
// Asynchronous readingfs.readFile('example.txt', 'utf8', (err, data) => { if (err) throw err; console.log(data);});
// Synchronous readingconst data = fs.readFileSync('example.txt', 'utf8');console.log(data);- Writing to a file:
const fs = require('fs');
fs.writeFile('output.txt', 'Hello, world!', (err) => { if (err) throw err; console.log('File saved');});The module also supports working with directories, streams, and access permissions.
2. Module path (Working with paths)
The path module simplifies manipulations with file paths and ensures cross-platform compatibility.
const path = require('path');
const fullPath = '/users/docs/file.txt';console.log(path.dirname(fullPath)); // /users/docsconsole.log(path.basename(fullPath)); // file.txtconsole.log(path.extname(fullPath)); // .txt
// Path constructionconst newPath = path.join('/users', 'docs', 'file.txt');console.log(newPath); // /users/docs/file.txt3. Module http (HTTP server and client)
With http, you can create web servers and send HTTP requests without third-party libraries.
- Simple HTTP server:
const http = require('http');
const server = http.createServer((req, res) => { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello from Node.js!');});
server.listen(3000, () => { console.log('Server running on port 3000');});- HTTP request (client):
const http = require('http');
http.get('http://api.example.com/data', (res) => { let data = ''; res.on('data', chunk => data += chunk); res.on('end', () => console.log(data));});4. Module stream (Data streams)
Streams allow you to process large volumes of data in chunks without loading everything into memory. Node.js supports four types of streams: Readable, Writable, Duplex, and Transform.
const fs = require('fs');const zlib = require('zlib');
// Reading from a file, compressing, and writingconst readStream = fs.createReadStream('input.txt');const writeStream = fs.createWriteStream('output.txt.gz');const gzip = zlib.createGzip();
readStream.pipe(gzip).pipe(writeStream);5. Module crypto (Cryptography)
The crypto module provides functionality for hashing, encryption, and random number generation.
const crypto = require('crypto');
// Password hashingconst hash = crypto.createHash('sha256').update('myPassword123').digest('hex');console.log(hash);
// Generating random bytescrypto.randomBytes(16, (err, buffer) => { console.log(buffer.toString('hex'));});6. Module events (Events)
Many objects in Node.js (for example, streams, servers) inherit from EventEmitter. This module allows you to create custom events
Also in library
Managing TypeScript Application Configuration: A Complete Guide
Protocol Buffers for Java: Complete Guide with Examples
Working with Excel in Rust: Libraries for Reading and Writing
PHP Application Configuration: Libraries and Approaches
Redis Client for Java: Jedis, Lettuce, and Redisson — Overview and Examples