Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File path in NextJS api route not resolving

Tags:

next.js

vercel

I'm trying to resolve a file path in NextJS.

I understand that API routes are working a little bit differently when deployed to Vercel. In order to create a correct path to the file I assumed I had to do this:

const svg = fs.readFileSync(
  path.join(process.cwd(), "img", "file.svg"),
  "utf-8",
);

// ENOENT: no such file or directory

But I cannot make it work. The file cannot be found under that path.

How can I find the correct path for a file in NextJS api routes?

I've followed the documentation of this.

  • Next version is: 11.1.3
  • When logging the path, it is giving /var/task/packages/project-root/img/file.svg
like image 846
supersize Avatar asked Sep 20 '25 13:09

supersize


1 Answers

For anyone coming across this, I actually opened a ticket at Vercel to ask them about this.

It turns out it was a caching issue that is caused by using Yarn 3

The support redirected me to this page explaining that they would have issues with anything above Yarn 1.

According to them there is nothing really they can do about right now but suggest us to use a different package manager.

like image 101
supersize Avatar answered Sep 23 '25 05:09

supersize