Node.js Core Modules

Node.js, being a server-side JavaScript runtime, comes with a set of core modules that provide essential functionality for building server-side applications. These core modules are built-in and can be used without the need for external installations. Here are some of the key Node.js core modules:

1. fs (File System):

  • Used for file operations such as reading from and writing to files.
  • Example:
    javascript const fs = require('fs'); fs.readFile('file.txt', 'utf8', (err, data) => { if (err) throw err; console.log(data); });

2. http:

  • Provides the functionality to create HTTP servers and make HTTP requests.
  • Example:
    javascript 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 listening on port 3000'); });

3. path:

  • Helps in working with file and directory paths.
  • Example:
    javascript const path = require('path'); const filePath = path.join(__dirname, 'files', 'example.txt');

4. events:

  • Provides an event emitter that allows objects to emit and listen for events.
  • Example:
    javascript const EventEmitter = require('events'); const myEmitter = new EventEmitter(); myEmitter.on('event', () => { console.log('Event emitted!'); }); myEmitter.emit('event');

5. os (Operating System):

  • Provides information about the operating system.
  • Example:
    javascript const os = require('os'); console.log(os.platform());

6. util:

  • Provides utility functions for formatting and debugging.
  • Example:
    javascript const util = require('util'); const debugLog = util.debuglog('example'); debugLog('This is a debug message');

7. querystring:

  • Helps in parsing and formatting URL query strings.
  • Example:
    javascript const querystring = require('querystring'); const params = querystring.parse('name=John&age=30');

8. crypto:

  • Provides cryptographic functionality, such as hashing and encryption.
  • Example:
    javascript const crypto = require('crypto'); const hash = crypto.createHash('sha256'); hash.update('Hello, World!'); console.log(hash.digest('hex'));

9. url:

  • Helps in parsing and formatting URLs.
  • Example:
    javascript const url = require('url'); const parsedUrl = url.parse('https://example.com/path?query=value');

10. zlib:

- Provides compression and decompression using the zlib library.
- Example:
  ```javascript
  const zlib = require('zlib');
  const data = Buffer.from('Hello, World!');
  zlib.deflate(data, (err, compressedData) => {
      if (err) throw err;
      console.log(compressedData);
  });
  ```

These are just a few examples of the core modules available in Node.js. There are many more modules that cater to different functionalities, and you can explore the official Node.js documentation for a comprehensive list and detailed documentation of each module: Node.js API Documentation.

Node.js comes with dozens of built-in modules. These built-in modules, sometimes referred to as core modules, give you access to tools for working with the file system, making http requests, creating web servers, and more

The module system is built around the require function. This function is used to load a module and get access to its contents. require is a global variable provided to all our Node.js scripts

Let’s look at an example

const fs = require(‘fs’)
fs.writeFileSync(‘notes.txt’, ‘I live in Philadelphia’)

The script above uses require to load in the fs module. This is a built-in Node.js module that provides functions you can use to manipulate the file system. The script uses writeFileSync to write a message to notes.txt

List of all core module in Node.js

Core ModuleDescription
httphttp module includes classes, methods and events to create Node.js http server.
urlurl module includes methods for URL resolution and parsing.
querystringquerystring module includes methods to deal with query string
pathpath module includes methods to deal with file paths
fsfs module includes classes, methods, and events to work with file I/O.
utilutil module includes utility functions useful for programmers.

Leave a Comment