Skip to content

Instantly share code, notes, and snippets.

@tomardern
Created January 25, 2025 21:50
Show Gist options
  • Save tomardern/bed716fcf34f453b653e37fb14684067 to your computer and use it in GitHub Desktop.
Save tomardern/bed716fcf34f453b653e37fb14684067 to your computer and use it in GitHub Desktop.
Hono middleware to set env variables in cloudflare workers

Ensure nodejs_compat is set in wrangler.json or wrangler.toml

"compatibility_flags": [ "nodejs_compat" ],

import * as process from 'node:process'
app.use((c, next) => {
Object.entries(c.env).forEach(([key, value]) => {
process.env[key] = value
})
next()
})
// Then use the following within the worker
import * as process from 'node:process'
console.log(process.env)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment