How to get image size from Next.js's API route (hosted on Vercel)?

Viewed 20

I need to get image dimensions from an API route of Next.js. Locally (both with npm run dev and npm run build && npm start) this (pseudo) code works:

import path from 'path';
import imageSize from 'image-size';

export default async function ApiEndpoint(req: NextApiRequest, res: NextApiResponse) {
  const { width, height } = imageSize(path.join('./public', photo));
  ... other things
}

On Vercel however it does not and I get this error:

Error: ENOENT: no such file or directory, open '/var/task/public/stories/tatiana.jpeg'

I've tried variations of the path (such as 'public', './public', '/public' or omitting "public") to no avail. Adding process.cwd() to the path leads to the same error.

Is there a recommended way how to read a file dynamically (not with import) in the API route of Next.js hosted on Vercel?

Thanks in advance!

0 Answers
Related