Core Modules in Node.js | Lecture 6

 

Here’s a detailed breakdown of Core Modules in Node.js, including the distinction between global and non-global modules:


Core Modules in Node.js

What are Core Modules?

  • Core modules are built-in modules provided by Node.js.
  • They are pre-compiled and optimized for performance.
  • No installation is required; you can directly use them with the require() function.

Global vs. Non-Global Core Modules

Global Core Modules:

  • Available without requiring an explicit require() statement.
  • These modules or functions are globally accessible.

Examples:

  1. console:
    • Used to print messages to the terminal.

o   console.log('Hello, World!');

  1. setTimeout and setInterval:
    • Scheduling functions.

o   setTimeout(() => console.log('Executed after 1 second'), 1000);

o   setInterval(() => console.log('Repeats every 2 seconds'), 2000);

  1. Buffer:
    • Used to work with binary data.

o   const buf = Buffer.from('Hello');

o   console.log(buf.toString()); // Outputs: Hello

  1. process:
    • Provides information and control over the current Node.js process.

o   console.log(process.platform); // Outputs: platform (e.g., 'win32', 'linux')

Non-Global Core Modules:

  • Must be explicitly imported using require() to use them.
  • Include modules like fs, http, path, etc.

Examples:

  1. fs (File System):
    • Provides file I/O operations.

o   const fs = require('fs');

o   fs.writeFileSync('file.txt', 'Hello, File System!');

  1. http (HTTP):
    • Enables creating HTTP servers.

o   const http = require('http');

o   const server = http.createServer((req, res) => {

o       res.end('Hello from HTTP module!');

o   });

o   server.listen(3000);

  1. path:
    • Utility module for handling file paths.

o   const path = require('path');

o   console.log(path.basename('/folder/example.txt')); // Outputs: example.txt

  1. os:
    • Provides operating system-related utilities.

o   const os = require('os');

o   console.log(os.type()); // Outputs: OS type (e.g., 'Linux', 'Windows_NT')


Complete List of Common Core Modules

  1. Global Modules:
    • console
    • setTimeout, clearTimeout
    • setInterval, clearInterval
    • Buffer
    • process
  2. Non-Global Modules:
    • File System (fs): File operations.
    • HTTP (http): Building HTTP servers.
    • HTTPS (https): Building HTTPS servers.
    • URL (url): Parsing and formatting URLs.
    • Path (path): File and directory path utilities.
    • OS (os): System-level information.
    • Query String (querystring): Parse and format query strings.
    • Crypto (crypto): Encrypting and hashing data.
    • Events (events): Event-driven programming.
    • Stream (stream): Work with data streams.
    • Zlib (zlib): Compress and decompress data.

Code Examples

1. Using Global Core Module (console and process):

console.log('Node.js version:', process.version);

console.log('Platform:', process.platform);

2. Using Non-Global Core Module (fs and path):

const fs = require('fs');

const path = require('path');

 

const filePath = path.join(__dirname, 'example.txt');

fs.writeFileSync(filePath, 'This is a test file.');

console.log(fs.readFileSync(filePath, 'utf-8'));

3. Using http to Create a Server:

const http = require('http');

 

const server = http.createServer((req, res) => {

    res.writeHead(200, { 'Content-Type': 'text/plain' });

    res.end('Hello, Core Modules!');

});

 

server.listen(3000, () => {

    console.log('Server is running on http://localhost:3000/');

});


Summary

Category

Examples

Requires require()

Global Modules

console, Buffer, process

No

Non-Global Modules

fs, http, path, os

Yes

Let me know if you’d like deeper explanations of specific modules or advanced use cases!

 

Comments

Popular posts from this blog

Introduction to Node.js | Lecture 1

Introduction to NPM | Lecture 2

Modules in Node.js | Lecture 4