Is it possible to access env without using c.env? #2764
-
I find it strange that I can only access the env variables inside a route or middleware handler via a context. Why can't I just use process.env or env helper without passing in the context? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
This is only the case when using Cloudflare Workers / Pages. This is not a limitation on Hono, but on how Cloudflare provides the environment variables. Here is how it is being handled in the background: export default {
async fetch(request, env) {
myHonoApp.fetch(request, env)
}; On each request made to a worker, the environment variables are also sent, that is why they are only accessible in context, they are not made available globally. |
Beta Was this translation helpful? Give feedback.
-
I got this problem too. How can I get these env without c.env, that means, why not just using global var or static API? |
Beta Was this translation helpful? Give feedback.
This is only the case when using Cloudflare Workers / Pages. This is not a limitation on Hono, but on how Cloudflare provides the environment variables. Here is how it is being handled in the background:
On each request made to a worker, the environment variables are also sent, that is why they are only accessible in context, they are not made available globally.