npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

tts-google

v1.0.3

Published

Google tts for Node.js

Readme

tts-google

A simple and efficient Google Text-to-Speech (TTS) library for Node.js that converts text to speech using Google's TTS service.

Features

  • 🎵 Convert text to speech using Google's TTS API
  • 🌍 Support for 50+ languages and language variants
  • 💾 Save audio to file (MP3 format)
  • 🔊 Stream audio data
  • 📦 Get audio as buffer for further processing
  • 🖥️ Built-in HTTP server for TTS service
  • 🚀 Promise-based API with async/await support

Installation

npm install tts-google

Dependencies

This package requires the following dependencies:

npm install multistream async fake-useragent request

Quick Start

const TTS = require('tts-google');

// Create TTS instance
const tts = new TTS('en'); // English

// Convert text to speech and save as file
async function example() {
    try {
        const filename = await tts.saveToFile('Hello World!', 'output.mp3');
        console.log(`Audio saved as: ${filename}`);
    } catch (error) {
        console.error('Error:', error);
    }
}

example();

API Reference

Constructor

const tts = new TTS(language)
  • language (string, optional): Language code (default: 'en')

Methods

saveToFile(text, filename)

Converts text to speech and saves as an audio file.

const filename = await tts.saveToFile('Hello World!', 'hello.mp3');

Parameters:

  • text (string): Text to convert to speech
  • filename (string): Output filename (MP3 format)

Returns: Promise that resolves to the filename

getAudioBuffer(text)

Converts text to speech and returns audio data as a Buffer.

const audioBuffer = await tts.getAudioBuffer('Hello World!');
console.log(`Buffer size: ${audioBuffer.length} bytes`);

Parameters:

  • text (string): Text to convert to speech

Returns: Promise that resolves to a Buffer containing audio data

playAudio(text)

Streams audio data (requires additional audio player implementation).

await tts.playAudio('Hello World!');

Note: This method provides a stream but requires an audio player like node-speaker to actually play the audio.

Static Methods

TTS.getAvailableLanguages()

Returns an object containing all supported languages.

const languages = TTS.getAvailableLanguages();
console.log(languages);
// Output: { 'en': 'English', 'es': 'Spanish', ... }

Supported Languages

The library supports 50+ languages including:

{
  'af': 'Afrikaans',
  'sq': 'Albanian', 
  'ar': 'Arabic',
  'hy': 'Armenian',
  'ca': 'Catalan',
  'zh': 'Chinese',
  'zh-cn': 'Chinese (Mandarin/China)',
  'zh-tw': 'Chinese (Mandarin/Taiwan)', 
  'zh-yue': 'Chinese (Cantonese)',
  'hr': 'Croatian',
  'cs': 'Czech',
  'da': 'Danish',
  'nl': 'Dutch',
  'en': 'English',
  'en-au': 'English (Australia)',
  'en-uk': 'English (United Kingdom)',
  'en-us': 'English (United States)',
  'eo': 'Esperanto',
  'fi': 'Finnish',
  'fr': 'French',
  'de': 'German',
  'el': 'Greek',
  'ht': 'Haitian Creole',
  'hi': 'Hindi',
  'hu': 'Hungarian',
  'is': 'Icelandic',
  'id': 'Indonesian',
  'it': 'Italian',
  'ja': 'Japanese',
  'ko': 'Korean',
  'la': 'Latin',
  'lv': 'Latvian',
  'mk': 'Macedonian',
  'no': 'Norwegian',
  'pl': 'Polish',
  'pt': 'Portuguese',
  'pt-br': 'Portuguese (Brazil)',
  'ro': 'Romanian',
  'ru': 'Russian',
  'sr': 'Serbian',
  'si': 'Sinhala (Sinhalese)',
  'sk': 'Slovak',
  'es': 'Spanish',
  'es-es': 'Spanish (Spain)',
  'es-us': 'Spanish (United States)',
  'sw': 'Swahili',
  'sv': 'Swedish',
  'ta': 'Tamil',
  'th': 'Thai',
  'tr': 'Turkish',
  'vi': 'Vietnamese',
  'cy': 'Welsh'
}

Advanced Usage

Multiple Languages

// English TTS
const ttsEn = new TTS('en');
const englishAudio = await ttsEn.getAudioBuffer('Hello World!');

// Spanish TTS
const ttsEs = new TTS('es');
const spanishAudio = await ttsEs.getAudioBuffer('¡Hola Mundo!');

// French TTS
const ttsFr = new TTS('fr');
const frenchAudio = await ttsFr.getAudioBuffer('Bonjour le monde!');

Error Handling

const tts = new TTS('en');

try {
    await tts.saveToFile('Your text here', 'output.mp3');
    console.log('Success!');
} catch (error) {
    if (error.message.includes('Language not supported')) {
        console.error('Invalid language code');
    } else {
        console.error('TTS Error:', error.message);
    }
}

Using with Express.js

const express = require('express');
const TTS = require('tts-google');

const app = express();
const tts = new TTS('en');

app.get('/speak', async (req, res) => {
    try {
        const text = req.query.text || 'Hello World!';
        const audioBuffer = await tts.getAudioBuffer(text);
        
        res.set({
            'Content-Type': 'audio/mpeg',
            'Content-Length': audioBuffer.length
        });
        res.send(audioBuffer);
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
});

app.listen(3000, () => {
    console.log('TTS Server running on port 3000');
});

Testing

Run the included tests:

npm test

The test suite includes:

  • Language availability check
  • TTS instance creation
  • Audio buffer generation
  • File saving functionality
  • Multi-language support

Technical Details

  • Text Limit: Each TTS request is limited to 100 characters. Longer text is automatically chunked.
  • Output Format: MP3 audio format
  • Rate Limiting: The library uses fake user agents to avoid rate limiting
  • Encoding: Supports UTF-8 text encoding

Troubleshooting

Common Issues

  1. "Language not supported" error

    • Check if your language code is in the supported languages list
    • Use lowercase language codes (e.g., 'en' not 'EN')
  2. Network errors

    • Ensure you have internet connectivity
    • Google's TTS service may occasionally be unavailable
  3. File permission errors

    • Make sure the output directory is writable
    • Check file path permissions

Getting Help

If you encounter issues:

  1. Check the GitHub Issues
  2. Run the test suite to verify functionality
  3. Ensure all dependencies are properly installed

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

ISC License

Author

K.Prabhasha - Evelocore


Keywords: tts, google, gtts, node, speech, voice, text-to-speech, audio, mp3