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:

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

Installation of gtts module:

  1. You can visit the link to Install gtts module. You can install this package by using this command.
    npm install gtts
  2. After installing gtts module, you can check your gtts version in command prompt using the command.
    npm version gtts
  3. 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

Filename: index.js

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Steps to run the program:

  1. The project structure will look like this:
    project structure
  2. Make sure you have installed gtts module using the following commands:
    npm install gtts
  3. Run index.js file using below command:
    node index.js

    Output of above command

  4. After running 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.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.