Node.js Core Packages
Essential Built-in Modules
Node.js core packages are fundamental modules built into Node.js. They offer essential functionalities for various development tasks, eliminating the need for external installations. This article details some of the most commonly used core modules.
Node.js Core Packages
Introduction
Node.js core packages are built-in modules that come bundled with the Node.js runtime environment, meaning they don't require separate installation. These modules provide essential functionalities for various tasks like file system operations, networking, and more. Examples include fs
(file system), http
, path
, os
, and events
.
Node.js Core Packages
fs (File System)
The fs
module allows interaction with the file system, enabling operations like reading, writing, and manipulating files and directories.
const fs = require('fs');
fs.readFile('myFile.txt', 'utf8', (err, data) => {
if (err) throw err;
console.log(data);
});
Example: Reading the content of a file. This code reads the file myFile.txt
asynchronously. If successful, it logs the content to the console; otherwise, it throws an error.
http
The http
module enables the creation of both HTTP servers and clients, facilitating communication over the web.
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World!');
});
server.listen(3000, () => {
console.log('Server running on port 3000');
});
Example: Creating a simple HTTP server that listens on port 3000 and responds with "Hello World!"
path
The path
module provides tools for working with file paths and directory structures.
const path = require('path');
const filePath = path.join('/home/user/documents', 'myFile.txt');
console.log(filePath); // Output: /home/user/documents/myFile.txt
Example: Joining path segments together safely.
os
The os
module offers information about the operating system.
const os = require('os');
console.log(os.platform()); // Output: win32, linux, darwin etc.
console.log(os.arch()); //Output: x64, arm64 etc.
Example: Getting the operating system platform and architecture.
events
The events
module facilitates event-driven programming.
const EventEmitter = require('events');
const emitter = new EventEmitter();
emitter.on('myEvent', (data) => {
console.log(data);
});
emitter.emit('myEvent', 'Event emitted!');
Example: Emitting and listening to custom events.
buffer
The buffer
module is used for working with raw binary data.
const buf = Buffer.from('Hello');
console.log(buf.toString()); // Output: Hello
Example: Creating a buffer from a string.
crypto
The crypto
module provides cryptographic functionality.
const crypto = require('crypto');
const hash = crypto.createHash('sha256');
hash.update('message');
console.log(hash.digest('hex'));
Example: Generating a SHA256 hash.
util
The util
module offers various utility functions.
const util = require('util');
console.log(util.format('%s %d', 'Hello', 123)); // Output: Hello 123
Example: Using util.format
for formatted output.
querystring
The querystring
module is used for parsing and formatting URL query strings.
const querystring = require('querystring');
const query = querystring.stringify({name: 'John', age: 30});
console.log(query); // Output: name=John&age=30
Example: Stringifying a query object.
net
The net
module enables low-level network communication using TCP sockets.
const net = require('net');
const server = net.createServer((socket) => {
socket.write('Hello from server!');
socket.end();
});
server.listen(8080);
Example: Creating a simple TCP server.
stream
The stream
module provides ways to handle streaming data.
const fs = require('fs');
const readStream = fs.createReadStream('myFile.txt');
readStream.on('data', (chunk) => {
console.log(chunk.toString());
});
Example: Reading a file using a readable stream.
url
The url
module offers tools for parsing and formatting URLs.
const url = require('url');
const myURL = new URL('https://example.com/path?query=string');
console.log(myURL.hostname); // Output: example.com
Example: Parsing a URL and accessing its components.
zlib
The zlib
module provides compression and decompression functionality.
const zlib = require('zlib');
const data = 'This is some sample data.';
zlib.gzip(data, (err, buffer) => {
if (!err) console.log(buffer.toString('base64'));
});
Example: Zipping the data using gzip.
Summary
fs
: File system operations.http
: HTTP server and client creation.path
: Path manipulation.os
: Operating system information.events
: Event-driven programming.buffer
: Handling raw binary data.crypto
: Cryptographic functions.util
: Utility functions.querystring
: Parsing and formatting URL query strings.net
: Low-level network communication.stream
: Stream data handling.url
: URL parsing and formatting.zlib
: Compression and decompression.