Built an OG endpoint that reads font files from node_modules/@fontsource/...
at render time. Used import.meta.url + fileURLToPath + path.resolve to
compute the absolute path relative to the source file.
The first build crashed with ENOENT trying to open
/.../dist/node_modules/@fontsource/.... Turns out Astro’s prerender
pipeline bundles endpoint source files into dist/.prerender/chunks/<hash>.mjs,
so import.meta.url points at the bundle, not the original source.
path.resolve(__dirname, '../..') goes up one level into dist/ instead
of the site root.
Fix: use process.cwd() as the anchor. During astro build (and during
pnpm build), process.cwd() is always the site’s package dir, because
that’s where the user invoked the script. Stable, well-defined, and
doesn’t depend on how the build pipeline bundles your code.
// Before (breaks in prerender):
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const PKG_ROOT = path.resolve(__dirname, '../..');
// After (stable):
const PKG_ROOT = process.cwd();
If you’re building endpoints that touch the filesystem at render time,
reach for process.cwd() by default.