Using fetch in an endpoint works well in development mode, but fails when executing the build using the node.js SSR adapter (@astrojs/[email protected]).
npm run build runs successfully. However, running either npm run preview or node dist/server/entry.mjs fails with the following error message:
file:///home/me/development/project-name/dist/server/chunks/pages/all.35ef890f.mjs:3021
fetch(thirdPartyURL + '?locale=en', fetchOptions)
^
ReferenceError: fetch is not defined
at getData (file:///home/me/development/project-name/dist/server/chunks/pages/all.35ef890f.mjs:2898:8)
at file:///home/me/development/project-name/dist/server/chunks/pages/all.35ef890f.mjs:2902:1
at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:533:24)
at async Module.preview (file:///home/me/development/project-name/node_modules/@astrojs/node/dist/preview.js:13:23)
at async preview (file:///home/me/development/project-name/node_modules/astro/dist/core/preview/index.js:33:18)
at async runCommand (file:///home/me/development/project-name/node_modules/astro/dist/cli/index.js:170:22)
at async cli (file:///home/me/development/project-name/node_modules/astro/dist/cli/index.js:180:5)
As a workaround, I have to import a node.js implementation of fetch from the dependency undici, which is included in Astro, inside the endpoint file:
import { fetch } from 'undici'
I am assuming that this is not the expected behaviour, and that fetch should be available in this context.
Any idea?
Open a New Issue