Open In App

How to Generate Borderless Table using ‘text-table’ NPM Module in Node.js ?

Last Updated : 10 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

text-table‘ is a very useful NPM module that can be used to generate borderless tables from the array of data. It’s a very simple module and you can easily create tables that are well-aligned, simple to read, etc. One of the main advantages of this module is its simplicity. With just a few lines of code, we can create a well-structured table and we can also customize its appearance according to our needs. The module supports a variety of options for configuring the table, including column alignment, padding, and border styles. 

Installation: To use any NPM module in your project or application, it should be installed first. Follow the given steps to install the module.

  • Open the terminal and navigate to the same directory of your project in which you want to use this module.
  • Enter the command with the module name to install this module which is given below.
  • When it gets installed, a JSON file will be added to your directory which contains all the details of the installed modules.
$ npm install text-table

Installation

Import the module: After installation, we have to import the module in our project or application using the given steps.

  • Import the module in your project using the require() function.
  • Pass the name of the module to this require function.
  • After successfully importing, the module will start working in the application or project.
  • A very important function table() is used to format the data in text format in tabular form.
var table = require('text-table');

 

Run the code: After installation, import, and write the code. We have to run the code file to see the output by using this command. Here, index.js is the name of the code file.

$ node index.js
  • This command will show the output of the code in which the module is used.

Example 1: Create a table with the details of the students

In this example, first, we have imported the module using the require function. Data is given in an array which is then formatted into tabular form using the table function and then we have displayed the output in the console using console.log

Javascript




// Import the module
const table = require('text-table');
  
const data = [
    ['Name', 'Branch', 'Roll number'],
    ['John', 'CSE', '01'],
    ['Jane', 'IT', '02'],
    ['Bob', 'CSE', '03']
];
  
const formattedTable = table(data);
  
console.log(formattedTable); // Output


Output:

Output 1

Example 2: Create a table with align property

In this example, first, we have imported the module. Then we have an array of data and we have also added align property to center the data in the table and then we displayed the table in the console using the console.log

Javascript




// Import the module
var table = require('text-table');
  
var t = table([
    ['GeeksforGeeks Courses', 'Starting Month'],
    ['Web Development', 'January'],
    ['App Development', 'February'],
    ['DSA', 'March']
], { align: ['c', 'c'] });
  
console.log(t); // Output


Output: 

Output 2



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads