Node.js external modules

CK CK 3788 
Created at
Updated at  
429 0 0

Node.js provides a wide variety of external modules that developers can use to add functionality to their applications. These modules are usually available via npm (Node Package Manager). Here are some commonly used external Node.js modules:

 

1. Express

  • Description: A fast, unopinionated, minimalist web framework for Node.js. It simplifies routing, middleware management, and handling HTTP requests and responses.
  • Installation: npm install express
  • Usage:

    const express = require('express');
    const app = express();
    
    app.get('/', (req, res) => {
      res.send('Hello World!');
    });
    
    app.listen(3000, () => {
      console.log('Server is running on port 3000');
    });
    

 

2. Mongoose

  • Description: An elegant MongoDB object modeling tool designed to work in an asynchronous environment.
  • Installation: npm install mongoose
  • Usage:

    const mongoose = require('mongoose');
    
    mongoose.connect('mongodb://localhost/my_database', {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    });
    
    const Schema = mongoose.Schema;
    const MyModel = mongoose.model('MyModel', new Schema({ name: String }));
    
    const doc = new MyModel({ name: 'Example' });
    doc.save();
    

 

3. Lodash

  • Description: A utility library that provides convenience functions for common programming tasks, such as manipulating arrays and objects.
  • Installation: npm install lodash
  • Usage:

    const _ = require('lodash');
    
    const numbers = [1, 2, 3, 4, 5];
    const evens = _.filter(numbers, n => n % 2 === 0);
    
    console.log(evens); // [2, 4]
    

 

4. Axios

  • Description: A promise-based HTTP client for the browser and Node.js, used for making requests to external APIs.
  • Installation: npm install axios
  • Usage:

    const axios = require('axios');
    
    axios.get('https://api.example.com/data')
      .then(response => {
        console.log(response.data);
      })
      .catch(error => {
        console.error('Error fetching data:', error);
      });
    

 

5. jsonwebtoken

  • Description: A module to sign and verify JSON Web Tokens (JWT), used for secure authentication in web applications.
  • Installation: npm install jsonwebtoken
  • Usage:

    const jwt = require('jsonwebtoken');
    
    const token = jwt.sign({ userId: 123 }, 'secretkey', { expiresIn: '1h' });
    
    jwt.verify(token, 'secretkey', (err, decoded) => {
      if (err) {
        console.error('Token verification failed:', err);
      } else {
        console.log('Decoded token:', decoded);
      }
    });
    

 

6. nodemail

  • Description: A module to send emails easily.
  • Installation: npm install nodemailer
  • Usage:

    const nodemailer = require('nodemailer');
    
    const transporter = nodemailer.createTransport({
      service: 'gmail',
      auth: {
        user: 'youremail@gmail.com',
        pass: 'yourpassword',
      },
    });
    
    const mailOptions = {
      from: 'youremail@gmail.com',
      to: 'receiver@example.com',
      subject: 'Hello',
      text: 'Hello world',
    };
    
    transporter.sendMail(mailOptions, (error, info) => {
      if (error) {
        console.error('Error sending email:', error);
      } else {
        console.log('Email sent:', info.response);
      }
    });
    

 

7. bcrypt

  • Description: A module to hash passwords and compare them, often used for secure password storage.
  • Installation: npm install bcrypt
  • Usage:

    const bcrypt = require('bcrypt');
    const saltRounds = 10;
    
    bcrypt.hash('myPassword', saltRounds, (err, hash) => {
      if (err) {
        console.error('Error hashing password:', err);
      } else {
        console.log('Hashed password:', hash);
      }
    });
    

These are just a few examples of the many external modules available in Node.js. Depending on your project's needs, there are countless others that can be utilized to streamline development and add advanced functionality.

Tags axios bcrypt express jsonwebtoken lodash mongoose nodemailer npm Facebook X
Comments 0
Similar posts
  1. Get client IP address and user agent in Node.js
    1,528
  1. Get client IP address and user agent in Node.js
    1,528
  2. forEach method in JavaScript when working with arrays
    665
  3. CRC32B function code in Javascript
    1,838
  4. Node.js-based Web Server supporting multiple path
    598
  5. A simple Javascript class example with static method for Node.js
    660
  6. Setting a Common Path for require() in Node.js
    993
  7. Import all JS files in a specific folder for Node.js programming
    1,723
  8. A simple example to access a PostgreSQL database in Node.js
    1,100
  9. Node.js code example to access Google BigQuery
    1,180
  10. Investigation on package.js for Node.js source code
    819
  11. Using MySQL in Node.js
    477
  12. A Node.js script that checks if a module is installed before requiring it
    769
  13. Worker Threads and Mutual Exclusion in Node.js
    459
  14. Using a module in the external class itself without creating an instance in Node.js
    349
  15. Multi-threaded Web Server based in Node.js
    863
  16. A basic HTTP server based in Node.js
    537
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    40
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    34
  3. How do I create animated background for Google Meet?
    20,611
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    244
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    94
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    157
  7. Array of arguments passed to script in PHP
    90
  8. Invalid Auction Storms in Ad Tech
    98
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    202
  10. The Two Clocks of Ad Server Architecture
    70
  11. Frequency Cap Architecture Overview
    514
  12. Collecting Proof-of-Play from 100,000 Screens
    146
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    287
  14. What is RedisVL?
    109
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    230
  16. TPU & GPU Cluster Management Tools
    243
  17. The Concept of Scaffolding in AI Agents
    233
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    583
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    258
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    339
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    202
  22. Machine Learning: Training vs. Ranking
    221
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    222
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    195
  25. Designing a Data Curation System Around a Vector Database
    220
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    256
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    656
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    195
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    217
  30. Understanding Data Lineage
    260