Vercel Analytics causing a crash on my Astro Project

Environment

  • Astro: 4.11.3
  • @astrojs/vercel: 7.7.2

Issue

I’m trying to deploy an Astro site to Vercel using Vercel Analytics, but I’m encountering an error related to the crypto module not being defined. This configuration worked until I upgraded packages recently.

Configuration

Here’s my astro.config.mjs:

import { defineConfig } from "astro/config";
import tailwind from "@astrojs/tailwind";
import vercel from "@astrojs/vercel/serverless";
import react from "@astrojs/react";

export default defineConfig({
  integrations: [tailwind(), react()],
  site: [MY DOMAIN HERE],
  output: "server",
  adapter: vercel({
    webAnalytics: { enabled: true },
  }),
});

The error I get says:

Unhandled Rejection: ReferenceError: crypto is not defined at decodeKey (file:///var/task/vercel/path0/.vercel/output/_functions/chunks/astro/server_D20XZRMg.mjs:1275:3) at deserializeManifest (file:///var/task/vercel/path0/.vercel/output/_functions/manifest_DlUM49nV.mjs:77:15) at file:///var/task/vercel/path0/.vercel/output/_functions/manifest_DlUM49nV.mjs:94:18 at ModuleJob.run (node:internal/modules/esm/module_job:195:25) at async ModuleLoader.import (node:internal/modules/esm/loader:337:24) at async importModuleDynamicallyWrapper (node:internal/vm/module:432:15) at async i (/opt/rust/nodejs.js:8:15915) Node.js process exited with exit status: 128. The logs above can help with debugging the issue.

I can comment out the webAnalytics line and it will run properly. Any suggestions?

Thanks!

Hey @stevegiralt. Which packages were updated, and what are the previous and current versions?

If you have a minimal reproducible example repo, that’s usually helpful to share so the rest of us can see exactly what you see.

node_modules/astro 4.13.1 → 4.13.2
@astrojs/compiler ^2.10.0->^2.10.1
node_modules/vite 5.3.5->5.4.0
node_modules/tailwindcss 3.4.7 → 3.4.9
node_modules/foreground-child 3.2.1 → 3.3.0

As for a reproducible example I can put one together and share asap

I would expect crypto to be supported with Node.js 20. What version of Node.js does your project use?

There’s a related Next.js issue with more info: `crypto` is undefined in API routes (Next13 appDir) · Issue #46852 · vercel/next.js · GitHub

Thanks so much for the replies and the possible issue. My project uses Node 18.x - I might have a fix in place, I am testing on another branch and it appears to be working -

The Astro documentation says to set my output to “server” , as does the Vercel documentation - but I believe this is causing problems. Once I switched my output to “hybrid” it appears to function as intended.

I’m currently monitoring and investigating this fix and making sure it doesn’t cause any other unintended issues.

I’m so glad you found a solution! Thank you for sharing it and please keep us updated on what you find :pray: