AssemblyAI / assemblyai-node-sdk

The AssemblyAI JavaScript SDK provides an easy-to-use interface for interacting with the AssemblyAI API, which supports async and real-time transcription, audio intelligence models, as well as the latest LeMUR models.
https://www.assemblyai.com
MIT License
38 stars 11 forks source link

ReferenceError: fetch is not defined #65

Closed vorvex closed 3 months ago

vorvex commented 3 months ago

Hello there,

I get the following error running assembly on a firebase cloud function:

ReferenceError: fetch is not defined at TranscriptService.fetch (/workspace/node_modules/assemblyai/dist/node.cjs:85:26) at TranscriptService.fetchJson (/workspace/node_modules/assemblyai/dist/node.cjs:105:37) at TranscriptService.submit (/workspace/node_modules/assemblyai/dist/node.cjs:500:33) at TranscriptService.transcribe (/workspace/node_modules/assemblyai/dist/node.cjs:462:39)

This is my CODE

` import { AssemblyAI, TranscriptUtterance } from 'assemblyai';

const client = new AssemblyAI({
  apiKey: '<token>',
});
const runTask = async (id: string, recording: Recording) => {
  try {
    if (recording.status !== 'pending') return;

    const pending = await db
      .collection('recordings')
      .doc(id)
      .get()
      .then((doc) => doc.exists && doc.data()?.status === 'pending');

    if (!pending) return;

    const data = {
      audio_url: recording.url,
      speaker_labels: true,
    };

    await db.collection('recordings').doc(id).update({
      status: 'processing',
    });

    const transcript = await client.transcripts.transcribe(data);
    console.log(transcript.text);

    await db.collection('recordings').doc(id).update({
      status: 'done',
      text: transcript.text,
      utterances: transcript.utterances,
    });
  } catch (error: any) {
    console.error(error);
    await db.collection('recordings').doc(id).update({
      status: 'failed',
      errorMessage: error.message,
    });
    return null;
  }
};

`

Thanks in Advance

Swimburger commented 3 months ago

Your Node.js runtime may be lower than 18. Node.js introduced Fetch natively since 18, which is the lowest version we support in the AssemblyAI SDK. 18 is also the lowest version supported by Node.js themselves, as older versions have fallen out of support.

I'm not sure what version of Node.js is configured on your Firebase Cloud Function, but I would look around in the configuration and see if you can update it.

Alternatively, you could use a package like node-fetch and add the fetch as a global variable.

vorvex commented 3 months ago

Thank you for the response. Seems it was still using version 16.