Open In App

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

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.



$ 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.

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

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




// 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




// 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


Article Tags :