Since a few days we have issues with relative url’s in vercels serverless functions. This problem only happpens on the vercel environment. The setup works locally with vercel dev.
Explanation:
I have serverless function X.js
FILE: /api/x.js
There is some code, in a specific condition it makes a call to another serverless function called y.js.
Both files exists, and it works locally, but since a few days it doesn’t work on the vercel environment.
When I replace the url to a full url, then it works again: const { data: testData } = await axios.post('https://some-vercel-domain.com/api/y')
Error log in Vercel:
Uncaught Exception: ReferenceError: DOMException is not defined
at Dt (/opt/rust/nodejs.js:8:12511)
at ClientRequest.<anonymous> (/opt/rust/nodejs.js:8:10713)
at Object.onceWrapper (node:events:628:26)
at ClientRequest.emit (node:events:525:35)
at ClientRequest.emit (node:domain:489:12)
at Socket.socketErrorListener (node:_http_client:494:9)
at Socket.emit (node:events:513:28)
at Socket.emit (node:domain:489:12)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
Node.js process exited with exit status: 129. The logs above can help with debugging the issue.
It’s good to hear that you have somewhat of a solution for now by using absolute URLs instead of relative ones.
However, to maintain flexibility across environments, it’s recommended to use environment variables or Vercel’s runtime configuration to dynamically construct the full URL.
For example, you can set a BASE_URL environment variable in your Vercel project settings and use it in your code like this: const { data: testData } = await axios.post($process.env.BASE_URL/api/y).
Your serverless functions should work correctly both in the Vercel environment and locally, solving the problem of relative URL resolution that’s causing your current errors in production.