Open In App

How to convert text to speech in Node.js ?

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:



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:

Filename: index.js 




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:

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

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

Article Tags :