Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

241
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda