Skip to content

Modules and packages in Node.js

What are modules in Node.js?

In Node.js, a module is a reusable block of code that can be loaded into another application or module. Modules can contain functions, variables, classes, and other code that can be used to add functionality to an application or to encapsulate code into logical units.

Modules are typically stored in separate files, with each file containing one or more modules. Modules are identified by their file names and can be loaded into an application using the require() function.

Importing and exporting modules

To use a module in Node.js, you need to import it into your application using the require() function. Here's an example of how to import a module:

const myModule = require('./myModule');

In this example, we're importing a module called myModule that is stored in a file called myModule.js.

To export a module from a file in Node.js, you need to use the module.exports object. Here's an example of how to export a module:

function myFunction() {
  // code goes here
}

module.exports = myFunction;

In this example, we're exporting a function called myFunction that can be used in other modules or applications.

Using packages and managing dependencies with NPM

In addition to modules, Node.js also has a package management system called NPM (Node Package Manager). NPM allows you to easily install and manage packages (collections of modules) that can be used in your Node.js applications.

To install a package using NPM, you can run the following command in your terminal or command prompt:

npm install packageName

This will download and install the specified package and any dependencies it requires.

To use a package in your application, you need to import it into your code using the require() function, just like with modules. Here's an example of how to import a package:

const express = require('express');

In this example, we're importing the express package, which is a popular web framework for Node.js.

NPM also allows you to manage dependencies for your own Node.js applications. You can create a package.json file that lists the dependencies for your application, and NPM will automatically install them when you run the npm install command.

Here's an example of a package.json file:

{
  "name": "myApp",
  "version": "1.0.0",
  "dependencies": {
    "express": "^4.17.1"
  }
}

In this example, we're specifying that our application depends on the express package, version 4.17.1 or higher. When we run npm install, NPM will download and install this package, along with any other dependencies specified in the package.json file.

An example: prompt module

This is an example program using the prompt module to ask the user for their name and age, and then print out a message with their information:

// index.js

// Import the prompt module
const prompt = require('prompt');

// Start the prompt
prompt.start();

// Get the name and age from the user
prompt.get(['name', 'age'], function(err, result) {
  if (err) {
    console.error(err);
    return;
  }

  // Print out a message with the user's information
  console.log(`Your name is ${result.name} and you are ${result.age} years old.`);
});

In this program, we use the prompt module to ask the user for their name and age. We pass an array of property names (['name', 'age']) to prompt.get(), which will prompt the user for those values. We also pass a callback function that will be called once the user has entered their information.

If there's an error while prompting the user, we log the error to the console. Otherwise, we use string interpolation to print out a message with the user's information.

To run this program, make sure you have installed the prompt module (you can do this with npm install prompt), and then run the program with node index.js. The program will prompt you for your name and age, and then print out a message with your information.

Accessing databases from Node.js: mysql2

mysql2 is a Node.js module that provides a MySQL database driver. The module uses the Node.js stream interface to communicate with the MySQL server, which allows for better performance and lower memory usage. mysql2 supports all the common MySQL operations such as querying, inserting, updating, and deleting data.

To install it run:

npm i mysql2

Supose we have the following database:

CREATE DATABASE IF NOT EXISTS `nodejs_sample`;

USE `nodejs_sample`;

CREATE TABLE IF NOT EXISTS `users` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `username` VARCHAR(50) NOT NULL,
  `email` VARCHAR(100) NOT NULL,
  `age` INT(11) NOT NULL,
  PRIMARY KEY (`id`)
);

INSERT INTO users (username, email, age) VALUES ('JohnDoe', 'johndoe@example.com', 28);
INSERT INTO users (username, email, age) VALUES ('JaneSmith', 'janesmith@example.com', 35);
INSERT INTO users (username, email, age) VALUES ('BobJohnson', 'bobjohnson@example.com', 42);
INSERT INTO users (username, email, age) VALUES ('AliceBrown', 'alicebrown@example.com', 23);

Basic operations

const mysql = require('mysql2');

// create the connection to the database
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'youruser',
  password: 'yourpassword',
  database: 'nodejs_sample'
});


// select data from a table
const sqlSelect = 'SELECT * FROM users';
connection.query(sqlSelect, function (err, results) {
  if (err) throw err;
  console.log(results);
});


// insert data into a table
const sqlInsert = 'INSERT INTO users (username, email) VALUES (?, ?)';
const values = ['JackDoe', 'johndoe@example.com'];
connection.query(sqlInsert, values, function (err, results) {
  if (err) throw err;
  console.log("Number of records inserted: " + results.affectedRows);
});

// update data in a table
const sqlUpdate = 'UPDATE users SET email = ? WHERE username = ?';
const updateValues = ['newemail@example.com', 'JohnDoe'];
connection.query(sqlUpdate, updateValues, function (err, results) {
  if (err) throw err;
  console.log("Number of records updated: " + results.affectedRows);
});

// delete data from a table
const sqlDelete = 'DELETE FROM users WHERE username = ?';
const deleteValues = ['JohnDoe'];
connection.query(sqlDelete, deleteValues, function (err, results) {
  if (err) throw err;
  console.log("Number of records deleted: " + results.affectedRows);
});

// close the connection to the database
connection.end();

In this example, we first create a connection to the database by passing in the database details. We then execute various SQL queries, including selecting data from a table, inserting data into a table, updating data in a table, and deleting data from a table.

Note that this is just a basic example and there are many more complex operations that can be done with the mysql2 module in Node.js.

Importing and exporting XML files into/from database

The fast-xml-parser module is a lightweight and high-performance XML parser for Node.js. It allows you to efficiently parse XML documents and convert them into JavaScript objects for further processing.

We can use fast-xml-parser to read and write XML files.

The following code is an example of exporting a database table to XML using mysql2, fs, and fast-xml-parser in Node.js:

const fs = require('fs');
const mysql = require('mysql2');
const { XMLBuilder } = require('fast-xml-parser');

// Create the database connection
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'youruser',
  password: 'yourpassword',
  database: 'yourdatabase'
});

// Select data from the database table
const sqlSelect = 'SELECT * FROM users';
connection.query(sqlSelect, (err, results) => {
  if (err) {
    console.error(err);
    return;
  }

  const builder = new XMLBuilder(
    {
        format: true
    }
  );

  // Convert the results to XML
  const xmlData = builder.build({
    root: {
        item: result
    }
  });

  // Write the XML string to a file
  fs.writeFile('data.xml', xmlString, (err) => {
    if (err) {
      console.error(err);
      return;
    }
    console.log('Data exported to data.xml');
  });

  // Close the database connection
  connection.end();
});
This script connects to the MySQL database, selects all data from the specified table, converts the results into an XML object using fast-xml-parser, and then converts the XML object to a formatted XML string. Finally, the XML string is written to a file named data.xml using the fs.writeFile function.

You can adjust the build options provided to fast-xml-parser to customize the XML output as per your requirements.

The result file should look like that:

<root>
  <item>
    <id>1</id>
    <username>JohnDoe</name>
    <email>johndoe@example.com</email>
    <age>28</age>
  </item>
  <item>
    <id>2</id>
    <username>JaneSmith</name>
    <email>janesmith@example.com</email>
    <age>35</age>
  </item>
  <item>
    <id>3</id>
    <username>BobJohnson</name>
    <email>bobjohnson@example.com</email>
    <age>42</age>
  </item>
</root>

This is an example of importing rows into a database table in Node.js:

const fs = require('fs');
const mysql = require('mysql2');
const { XMLParser } = require('fast-xml-parser');


// Create the database connection
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'youruser',
  password: 'yourpassword',
  database: 'yourdatabase'
});

// Read the XML file
fs.readFile('data.xml', 'utf-8', (err, xmlData) => {
  if (err) {
    console.error(err);
    return;
  }

  const parser = new XMLParser();

  // Parse the XML data
  const parsedData = parser.parse(xmlData);

  // Extract the items from the parsed XML object
  const items = parsedData.root.item;

  // Insert the data into the database table
  const sqlInsert = 'INSERT INTO users (username, email, age) VALUES (?, ?, ?)';
  items.forEach(item => {
    const values = [item.username, item.email, item.age];
    connection.query(sqlInsert, values, (err, results) => {
      if (err) {
        console.error(err);
        return;
      }
      console.log('Record inserted:', results.insertId);
    });
  });

  // Close the database connection
  connection.end();
});
This script reads the XML file (data.xml), parses its contents using fast-xml-parser, and extracts the necessary data. Then, it inserts the extracted data into the specified table in the MySQL database using parameterized queries. Finally, it logs the insert IDs of the inserted records and closes the database connection.