Skip to content
Back

ReferenceError: crypto is not defined

  • 0
  • 6
  • Functions
  • Cloud
Zionnite
3 Oct, 2024, 19:04

here is my code

TypeScript
import { default as axios } from 'axios';
import fetch from 'node-fetch';
import { WebhookReceiver } from 'livekit-server-sdk';

// This is your Appwrite function
// It's executed each time we get a request
export default async ({ req, res, log, error }) => {
  const client = new Client();
  const functions = new Functions(client);
  const database = new Databases(client);
  
  client.setEndpoint('https://cloud.appwrite.io/v1')
    .setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID)
    .setKey(process.env.APPWRITE_API_KEY);

  const ENDPOINT                                = process.env.MY_APPWRITE_ENDPOINT;
  const PROJECT_ID                              = process.env.APPWRITE_PROJECT_ID;
  const API_KEY                                 = process.env.APPWRITE_API_KEY;
  const DATABASE_ID                             = process.env.DATABASE_ID;
  const LIVEKIT_STREAMING_COLLECTION            = process.env.LIVEKIT_STREAMING_COLLECTION;
  const LIVE_KIT_API                            = process.env.LIVE_KIT_API;
  const LIVE_KIT_SEC_KEY                        = process.env.LIVE_KIT_SEC_KEY;
  const LIVE_KIT_WEBSOCKET                      = process.env.LIVE_KIT_WEBSOCKET;


  const receiver = new WebhookReceiver(LIVE_KIT_API, LIVE_KIT_SEC_KEY);

  if (req.method === 'POST') {
    const jsondData    = JSON.parse(req.bodyRaw);
    const VideoLibraryId    = jsondData.VideoLibraryId;
    const VideoGuid         = jsondData.VideoGuid;
    const Status            = jsondData.Status;

    // Event is a WebhookEvent object
    var headers = req.headers;
    log(`headers ${headers}`);
    var authorization = headers['authorization'];
    // const authorization = req.headers["Authorization"];
    const event = await receiver.receive(req.body, authorization);
    log(`Events ${event}`);

  }

  return res.send({method: 'method is POST'});
};
TL;DR
Users are encountering a ReferenceError because "crypto" is not defined. To resolve this, add the following code at the top of your function: ```javascript import { webcrypto } from 'node:crypto'; globalThis.crypto ??= webcrypto; ```
Zionnite
3 Oct, 2024, 19:06

I'm using NodeJs Runtime Environment 18.0

Zionnite
3 Oct, 2024, 19:34

@Joshi hello

Joshi
3 Oct, 2024, 19:37

You literally put the answer to your problem in the title

Zionnite
3 Oct, 2024, 19:44

my head is smoking and confused...

Zionnite
3 Oct, 2024, 19:49

it seem like i need a higher nodejs runtime

3 Oct, 2024, 21:14

Please what are your thoughts on this

4 Oct, 2024, 01:11

Thank you very much Steve, Please how do I add the --experimental-global-webcrypto flag when using Node.js

4 Oct, 2024, 01:11

<@462046107556511744>

4 Oct, 2024, 01:33

or is there way i can upgrade my nodejs 18 to 19 in Appwrite?

4 Oct, 2024, 06:11

Hello <@462046107556511744>

4 Oct, 2024, 08:40

any support please/

5 Oct, 2024, 08:35

<@462046107556511744> <@777466243535994910> please is there hope on resloving this ?

16 Oct, 2024, 01:42

Add this at the top of your function:

import { webcrypto } from 'node:crypto';

globalThis.crypto ??= webcrypto;

16 Oct, 2024, 11:52

<@1144658630591909998>

28 Mar, 2026, 18:45
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more