Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

240
Vistas
How to load images in express js hosted on vercel

I am trying to make a GitHub banner generator using express js, my code works fine locally but when I try to host it on vercel all the image files go missing and I get no such file or directory found error.

app.get('/api',async(req,res)=>{

    const title = req.query.title || "Github Banner Generator"
    const subTitle = req.query.subtitle || ""
    
    const theme = req.query.theme || "default"

    const _theme = require(`./themes/${theme}/${theme}.json`)

    const image = await loadImage(fs.readFileSync('themes/default/image1.png'))

    const img = await new Canvas(1280, 520)
    .printImage(image,0,0)
    .setColor(_theme.primaryColor)
    .setTextFont('bold 60px Serif')
    .setTextAlign('center')
    .printText(title,1280/2,520/2)
    .setTextFont('20px Serif')
    .setTextAlign('center')
    .printText(subTitle,1280/2,(520/2)+60)
    .toBuffer();

    res.set({'Content-Type':'image/png'})
    res.send(img)
})

error screenshot

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I think the image path isn't right because of you didn't used an absolute path. You should read more about the difference between ./ and __dirname in NodeJS.

My solution would be to use __dirname + (the file location depending on the current file folder)

const image = await loadImage(fs.readFileSync(__dirname 
+ '/themes/default/image1.png'));

Here, I assume that your project structure looks something like this...

/package.json
/index.js (contains the code)
/themes/default/image1.png
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda