Monorepo on Cloudflare Workers: Unable to pass down Environment Variables
Hey guys, new to Cloudflare Workers, Wrangler, and D1.
Trying to scaffold out to use Cloudflare Workers in a monorepo structure, and get it deployed:
Monorepo Structure:
I'm using Vite (React), Drizzle, Better-Auth, and Hono.
apps/server/wrangler.toml:
apps/web/wrangler.toml:
I had a few questions:
- How does my web/ monorepo get access to the workers environment variables?
When I check the settings under Environment Variables, it says that static assets do not have access to environment variables.
- How does my db/ package (which is Drizzle), gain access to the D1 instance? This is mainly for running the cli, i.e. migration, pushing.4 Replies
The variables are defined in each wrangler.toml (i.e. web and server). The worker that runs in web has access to the variables defined in web/wrangler.toml and likewise for the server worker
For local development you define the variables in .dev.vars in each app. In order for the variables to be available in the runtime context you have to run
wrangler types firstyou can also set your envs as secrets.
https://developers.cloudflare.com/workers/configuration/secrets/
for drizzle configuration (for migrations), its a little bit more harder
you can see these docs:
https://orm.drizzle.team/docs/connect-cloudflare-d1
https://orm.drizzle.team/docs/guides/d1-http-with-drizzle-kit
at my personal projects, i use this configuration (to allow me to apply migration at local/production enviroments)
here a axample configration
drizzle.config.ts
Drizzle ORM - Cloudflare D1
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Drizzle ORM - Cloudflare D1 HTTP API with Drizzle Kit
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
at your wrangler.jsonc (se the drizle/d1 connections envs)
also, you can macro the drizle commands at your package.json