I'm getting this weird error: Cannot read properties of undefined (reading 'node')

Lollykrown
Lollykrown Member Posts: 3
edited August 28 in Developer APIs

I'm using cloudinary nodejs api in a next.js 14 server actions. It has been working perfectly until recently.

When I remove cloudinary from the project, it works perfectly, whenever i add it, i get this error "TypeError: Cannot read properties of undefined (reading 'node')".

Please advise

Tagged:

Answers

  • Wissam
    Wissam Member, Cloudinary Staff Posts: 103

    Hi @Lollykrown ,

    The error is likely occurring because the Cloudinary library is trying to access a Node.js environment, but Next.js 14 with Server Actions runs in a different environment called the Edge Runtime.

    Could you share the code that throws this error?
    I have checked this type of error and I found the following question in Stackoverflow:
    https://stackoverflow.com/questions/78634470/typeerror-cannot-read-properties-of-undefined-reading-node

    Best Regards,

    Wissam

  • Lollykrown
    Lollykrown Member Posts: 3

    'use server'

    import { v2 as cloudinary } from 'cloudinary'; //deleting this line stops the error

    import { randomString } from '@/utils/misc';

    import pLimit from 'p-limit';

    const limit = pLimit(5)

    if (!process.env.CLOUDINARY_CLOUD_NAME) { throw new Error('CLOUDINARY_CLOUD_NAME is not set');}

    if (!process.env.CLOUDINARY_API_KEY) { throw new Error('CLOUDINARY_API_KEY is not set');}

    if (!process.env.CLOUDINARY_API_SECRET) { throw new Error('CLOUDINARY_API_SECRET is not set');}

    cloudinary.config({

    cloud_name: process.env.CLOUDINARY_CLOUD_NAME,

    api_key: process.env.CLOUDINARY_API_KEY,

    api_secret: process.env.CLOUDINARY_API_SECRET,

    });

    export async function uploadBuffer(image) {

    const imageData = await image.arrayBuffer();

    const mime = image.type; const encoding = 'base64';

    const base64Data = Buffer.from(imageData).toString('base64');

    const fileUri = 'data:' + mime + ';' + encoding + ',' + base64Data; const result = await cloudinary.uploader.upload(fileUri, {

    folder: 'tiviTea folder',

    public_id: `tiviTea-${randomString(10)}`,

    });

    return result.secure_url;

    }

    export async function bulkUploadBuffer(images) {

    const res = images.map(image =>{

    return limit(async() => {

    const imageData = await image.arrayBuffer();

    const mime = image.type;

    const encoding = 'base64';

    const base64Data = Buffer.from(imageData).toString('base64');

    const fileUri = 'data:' + mime + ';' + encoding + ',' + base64Data;

    const result = await cloudinary.uploader.upload(fileUri,{

    folder: 'tiviTea folder',

    public_id: `tiviTea-${randomString(10)}`,

    return result.secure_url;

    })

    })

    let uploads = await Promise.all(res) return uploads;

    }

  • remi
    remi Member Posts: 2

    Did you find a fix? Im getting the same error when using sdk in Cloudflare workers

  • Lollykrown
    Lollykrown Member Posts: 3

    No. I had to find another way

  • remi
    remi Member Posts: 2

    Another way as in: fetching the api directly?