Skip to main content

Node.js setup

Node.js 20 or later is required.

import {writeFile} from 'node:fs/promises';
import {KittenTTS} from '@kittentts/web';

const tts = await KittenTTS.create({
model: 'nano-int8',
storageDirectory: '.cache/kittentts',
});

const result = await tts.generate('Generated in Node.js.');
await writeFile('speech.wav', result.wavData());
await writeFile('speech.mp3', await result.mp3Data({bitRate: 128}));
await tts.dispose();

Express response

app.post('/speech', async (req, res) => {
const result = await tts.generate(req.body.text, {voice: 'bella'});
res.type('audio/wav').send(Buffer.from(result.wavData()));
});

Create the TTS instance once during service startup and reuse it. Constructing it for each request repeats asset and ONNX initialization work.

The repository's examples/node-express project downloads model, voices, and phonemizer data into .cache/kittentts on first run:

cd examples/node-express
npm install
npm start

Node uses filesystem asset storage by default. Use local model paths when server startup cannot depend on external downloads.