Next.js Discord

Discord Forum

Access a font file from api route

Answered
Cape lion posted this in #help-forum
Open in Discord
Cape lionOP
Hey everyone! I'm trying to access a font file from my api route and I'm not so lucky with it. The file font is under public/fonts/font.ttf. In development mode I can access the file using public/fonts/font.ttf but when I deploy the app at vercel I get the error: Error: ENOENT: no such file or directory, open 'public/fonts/font.ttf'. I tried change the path to fonts/font.ttf but get a similar error than before.
I tried using path too, like this: path.join(process.cwd(), 'fonts', 'font.ttf') but even with this I get the same error as before.
Error: ENOENT: no such file or directory, open '/var/task/fonts/font.ttf'

How could I access this file after deployment?
Answered by Cape lion
I found the answer. The approach is kind simple. Using a friend past advice, I moved the fonts folder to the root project. After this, add this code line to switch the get path between enviroments: const fontPath = process.env.NODE_ENV === 'development' ? 'fonts/Yellowtail-Regular.ttf' : path.join(process.cwd(), 'fonts', 'Yellowtail-Regular.ttf');.
Thx for the help!
View full answer

1 Reply

Cape lionOP
I found the answer. The approach is kind simple. Using a friend past advice, I moved the fonts folder to the root project. After this, add this code line to switch the get path between enviroments: const fontPath = process.env.NODE_ENV === 'development' ? 'fonts/Yellowtail-Regular.ttf' : path.join(process.cwd(), 'fonts', 'Yellowtail-Regular.ttf');.
Thx for the help!
Answer