Bun and Environment Variables - how?

I'm trying to read environment variables and everything I try seems to fail. Here are some of the attempts and issues. - Using import { env } from "bun"; results in ERROR: Could not resolve "bun". - const test_env = process.env.TEST_ENV; results in Uncaught ReferenceError: process is not defined
8 Replies
Walshy
Walshy5mo ago
can't speak much to bun but it seems like you don't have bun installed there and are trying to run it from nodejs as for the second, is process imported? and are you doing these at build time or runtime?
Magick
MagickOP5mo ago
These errors occur when running wrangler deploy --minify src/index.ts. There is no explicit import of process. Though my understanding of CF pages is that it isnt supported anyway, but I cannot see any documentation on what to use as an alternative.
Walshy
Walshy5mo ago
so this is runtime, the Worker (this is also a Worker not Pages at least, you're trying to deploy as a Worker) runtime is v8 not jscore/bun. So you can't use bun apis as for process, process is not implemented yet in the nodejs support - https://developers.cloudflare.com/workers/runtime-apis/nodejs/#nodejs-compatibility to access env vars, you use the env object passed into handlers - https://developers.cloudflare.com/workers/runtime-apis/bindings/ and if this is meant to be pages functions then you do it on the context.env object - https://developers.cloudflare.com/pages/functions/bindings/
Magick
MagickOP5mo ago
Ok, this is very helpful. Thanks. This is probably outside of your area, but if I need to use different syntax for environment variables in dev and when deployed, how best to handle these differences?
Walshy
Walshy5mo ago
what do you mean? different how?
Magick
MagickOP5mo ago
The example in one of your links is:
export interface Env {
API_HOST: string;
}

export default {
async fetch(request, env, ctx): Promise<Response> {
return new Response(`API host: ${env.API_HOST}`);
}
} satisfies ExportedHandler<Env>;
export interface Env {
API_HOST: string;
}

export default {
async fetch(request, env, ctx): Promise<Response> {
return new Response(`API host: ${env.API_HOST}`);
}
} satisfies ExportedHandler<Env>;
If I try to add this exact code I get an error of Cannot find name ExportedHandler .. There is no mention of where or how to import this. If I try to use env.TEST_ENV without importing bun then I get Cannot find name 'env'.. To put it another way, Bun says it supports reading environment variables in the following ways:
process.env.API_TOKEN; // => "secret"
// or
Bun.env.API_TOKEN; // => "secret"
process.env.API_TOKEN; // => "secret"
// or
Bun.env.API_TOKEN; // => "secret"
Neither of these work. What does work in the CF Worker doesnt work in Bun locally.
Walshy
Walshy5mo ago
well yeah because bun isn't using the same apis what's the actual code you're using? and you can test it with wrangler dev you don't need bun involved at all
Magick
MagickOP5mo ago
Ok, that helps. And the code on this issue also helps - https://github.com/elysiajs/elysia/issues/368
GitHub
Cloudflare Pages/Worker Example · Issue #368 · elysiajs/elysia
The documentation briefly mentions Cloudflare, but Wrangler was a bit more annoying to set up than I expected. Should I add a documentation section or can I just somehow link to an example? Regardl...
Want results from more Discord servers?
Add your server