node.jsfontsvercelnode-canvas

node-canvas registerFont can't find font file once deployed (works locally)


I have a Node.js server that uses node-canvas to render text on an image on the server-side. Here is the repo: https://github.com/shawninder/meme-generator (just git clone, npm i and npm run dev to run locally).

As you'll notice in the code, I am loading the Anton font, which I got from here with the documented registerFont function provided by node-canvas

registerFont('./fonts/Anton-Regular.ttf', { family: 'Anton' })

Everything works like a charm locally, but when I deploy to Vercel (formerly known as zeit), that line throws an ENOENT error:

no such file or directory, lstat '/var/task/fonts'


Solution

  • The solution ended up being

    import path from 'path'
    registerFont(path.resolve('./fonts/Anton-Regular.ttf'), { family: 'Anton' })`
    

    See path.resolve