Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

239
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!