- Node.js Tutorial
- NodeJS Home
- NodeJS Introduction
- NodeJS Setup
- NodeJS First App
- NodeJS REPL
- NodeJS Command Line
- NodeJS NPM
- NodeJS Callbacks
- NodeJS Events
- NodeJS Event-Loop
- NodeJS Event-Emitter
- NodeJS Global-Objects
- NodeJS Console
- NodeJS Process
- NodeJS Buffers
- NodeJS Streams
- Node.js File Handling
- Node.js File System
- Node.js Read/Write File
- Working with folders in Node.js
- HTTP and Networking
- Node.js HTTP Module
- Anatomy of an HTTP Transaction
- Node.js MongoDB
- MongoDB Get Started
- MongoDB Create Database
- MongoDB Create Collection
- MongoDB Insert
- MongoDB Find
- MongoDB Query
- MongoDB Sort
- MongoDB Delete
- MongoDB Update
- MongoDB Limit
- MongoDB Join
- Node.js MySQL
- MySQL Get Started
- MySQL Create Database
- MySQL Create Table
- MySQL Insert Into
- MySQL Select From
- MySQL Where
- MySQL Order By
- MySQL Delete
- MySQL Update
- MySQL Join
- Node.js Modules
- Node.js Modules
- Node.js Built-in Modules
- Node.js Utility Modules
- Node.js Web Module
- Node.js Advanced
- Node.js Debugger
- Node.js Scaling Application
- Node.js Packaging
- Node.js Express Framework
- Node.js RESTFul API
- Node.js Useful Resources
- Node.js Useful Resources
- Node.js Discussion
Node.js Built-In Modules
Node.js provides a rich set of built-in modules that offer core functionalities to build applications without needing external libraries. These modules are directly available without installation, making development faster and more efficient.
Key Features of Built-In Modules
- Predefined functionality like file handling, networking, and more are readily available.
- Built-in modules are part of the Node.js runtime and need no installation.
- Optimized for speed and reliability, ensuring high performance.
- Require minimal setup and offer intuitive APIs for developers.
Commonly Used Built-In Modules
Here are some popular built-in modules in Node.js:
- fs (File System): Handles file operations such as reading, writing, and deleting files.
- http: Enables creating HTTP servers and handling requests/responses.
- path: Simplifies working with file paths.
- os: Offers system-related utilities like CPU and memory information.
- events: Manages events using the EventEmitter class.
- util: Includes utility functions like formatting and debugging.
Example Code
Using the fs Module
const fs = require('fs');
// Writing to a file
fs.writeFileSync('example.txt', 'Hello, World!');
console.log('File written successfully.');
// Reading from a file
const data = fs.readFileSync('example.txt', 'utf8');
console.log('File content:', data);
This example demonstrates writing to and reading from a file using the fs module.
Using the http Module
const http = require('http');
// Create an HTTP server
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, World!\n');
});
server.listen(3000, () => {
console.log('Server running at http://localhost:3000/');
});
This example shows how to create a simple HTTP server that responds with "Hello, World!".
Using the path Module
const path = require('path');
// Join paths
const fullPath = path.join('/users', 'john', 'docs', 'file.txt');
console.log('Full Path:', fullPath);
// Get file extension
const ext = path.extname('file.txt');
console.log('File Extension:', ext);
This example demonstrates working with file paths using the path module.
Using the os Module
const os = require('os');
console.log('Operating System:', os.type());
console.log('Total Memory:', os.totalmem());
console.log('Free Memory:', os.freemem());
console.log('CPU Info:', os.cpus());
This example retrieves information about the operating system and hardware using the os module.
Summary
Node.js built-in modules provide essential functionalities for building applications without requiring additional libraries. Popular modules like fs, http, path, os, and events simplify operations ranging from file handling to creating servers. These modules are pre-installed, optimized, and ready to use, making development in Node.js efficient and powerful.