Open In App

How to convert text to speech in Node.js ?

Improve
Improve
Like Article
Like
Save
Share
Report

To convert text to speech in Node.js, there are various modules but the most popular among them is gtts (Google Text to Speech) module. 

Feature of gtts module:

  • It is easy to get started and easy to use.
  • It is widely used and popular for converting text to speech.

Installation of gtts module:

You can visit the link to Install gtts module. You can install this package by using this command.

npm install gtts

After installing gtts module, you can check your gtts version in the command prompt using the command.

npm version gtts

After that, you can create a folder and add a file, for example, index.js. To run this file you need to run the following command.

node index.js

Project Structure:

project structure

Filename: index.js 

javascript




const gTTS = require('gtts');
     
let speech = 'Welcome to GeeksforGeeks';
const  gtts = new gTTS(speech, 'en');
 
gtts.save('Voice.mp3', function (err, result){
    if(err) { throw new Error(err); }
    console.log("Text to speech converted!");
});


Steps to run the program:

Make sure you have installed gtts module using the following commands:

npm install gtts

Run the index.js file using the below command:

node index.js

Output:

Output of above command

After running the above command, your text is converted to speech and save in your Voice.mp3 file as shown below: Output of above command

So this is how you can use the gtts (Google Text to Speech) module for converting text to speech in Node.js.


Last Updated : 27 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads