Onjsdev

Share


How to Create A Simple Progress Bar In Nodejs


By onjsdev

Nov 8th, 2023

Progress bars are an essential tool for monitoring the progress of long-running processes in applications. In Node.js, progress bars can be created using various modules and libraries available in the Node Package Manager (npm).

In this article, we will explore how to create a basic progress bar in Node.js using the "progress" module.

The first step is to install the "progress" module using npm. Open the terminal and type the following command:

npm install progress

After installing the "progress" module, we can create a progress bar by using its constructor function.

const ProgressBar = require("progress");

const progressBar = new ProgressBar(":bar :percent", {
  total: 10,
  width: 20,
  complete: "=",
  incomplete: "-",
  callback: function () {
    clearInterval(perform);
    console.log('Process completed!');
  },
});

After creating the progress bar, we can update its progress using the tick() method. This method takes an optional argument, which is the number of steps to increment the progress by.


const perform = setInterval(() => {
  progressBar.tick();
}, 1000);
// Perform long-running task here

Conclusion

In this article, we have learned how to create a progress bar in Node.js using the "progress" module. By customizing the options provided by the module, we can create a progress bar that fits our specific needs.

Thank you for reading.