Edge TTS for Node.js
node-edge-tts is a Node.js module providing an interface to Microsoft Edge's free online Text-to-Speech (TTS) service. Currently at version 1.2.10, this library allows developers to convert text into speech without needing an API key, relying on the public Edge TTS endpoint. It offers both a programmatic API for integrating TTS into Node.js applications and a command-line interface for quick conversions. Key features include configurable voices, languages, output formats, and the ability to save detailed subtitles alongside the audio. Unlike cloud-based TTS solutions that often require authentication and consumption-based billing, node-edge-tts leverages a readily available service, making it suitable for projects requiring an accessible and free text-to-speech option, though with potential limitations inherited from the underlying Edge service regarding supported speech configurations. The project appears to be actively maintained, with regular updates indicated by its current version number.
Common errors
-
Error: Network request failed
cause The module could not connect to the Microsoft Edge TTS service endpoint due to network issues, firewall restrictions, an incorrect proxy configuration, or a timeout.fixVerify your internet connection, check firewall settings, ensure any specified `proxy` option is correct and reachable, and consider increasing the `timeout` option for slower networks. -
Error: Invalid voice or language parameter
cause The `voice` or `lang` specified in the configuration is not recognized or supported by the Microsoft Edge TTS service, or it uses an incorrect format.fixRefer to Microsoft's documentation for supported voices and languages (linked in the README) and ensure the exact string is used. Test with common, well-known voices first (e.g., 'en-US-AriaNeural'). -
ENOENT: no such file or directory, open './output.mp3'
cause The directory specified in the `filepath` for the output audio does not exist, and the library does not automatically create it.fixBefore calling `ttsPromise`, ensure the directory for the output file exists. Use `fs.mkdirSync(path.dirname(audioFilePath), { recursive: true });` if the directory might not exist.
Warnings
- gotcha Some advanced speech configuration options (pitch, rate, volume) listed in Microsoft's official documentation might not be fully supported by the underlying Edge online TTS service this library uses, leading to unexpected behavior or ignored settings.
- gotcha The library relies on an unofficial interface to Microsoft Edge's online TTS service. This means its functionality can be broken by undocumented changes to Edge's service or internal APIs, which are outside the control of the library maintainers.
- gotcha Saving subtitles generates a JSON file with the same base name as the audio file in the specified output directory. Ensure your application handles both files appropriately, especially for cleanup or relocation.
Install
-
npm install node-edge-tts -
yarn add node-edge-tts -
pnpm add node-edge-tts
Imports
- EdgeTTS
const EdgeTTS = require('node-edge-tts')import { EdgeTTS } from 'node-edge-tts' - EdgeTTS
import EdgeTTS from 'node-edge-tts'
const { EdgeTTS } = require('node-edge-tts') - EdgeTTSOptions
import type { EdgeTTSOptions } from 'node-edge-tts'
Quickstart
import { EdgeTTS } from 'node-edge-tts';
import * as path from 'path';
import * as fs from 'fs';
async function generateSpeech() {
const outputDir = './audio_output';
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
const audioFilePath = path.join(outputDir, 'hello_world.mp3');
const tts = new EdgeTTS({
voice: 'en-US-AriaNeural',
lang: 'en-US',
outputFormat: 'audio-24khz-96kbitrate-mono-mp3',
saveSubtitles: true,
// proxy: 'http://localhost:7890', // Uncomment and configure if behind a proxy
// timeout: 15000 // Increase timeout if network is slow
});
try {
console.log(`Generating speech to ${audioFilePath}...`);
await tts.ttsPromise('Hello world, this is a test from node-edge-tts!', audioFilePath);
console.log('Speech generated successfully!');
console.log(`Subtitles saved to ${audioFilePath.replace('.mp3', '.json')}`);
} catch (error) {
console.error('Failed to generate speech:', error);
}
}
generateSpeech();