hono: .env files not loaded into c.env

I found the .env files are not loaded into c.env:

Runtime: Bun 0.4.0

// index.ts

import { Hono } from 'hono'

interface Bindings {
  API_KEY: string
  API_SECRET: string
}

const app = new Hono<{ Bindings: Bindings }>()

app.get('/', async (c) => {
  console.log(c.env.API_KEY) // <--- undefined
  return c.text('hello')
})

export default {
  port: 3000,
  fetch: app.fetch,
}
# .env

API_KEY=hono
API_SECRET=123

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 2
  • Comments: 17 (8 by maintainers)

Most upvoted comments

Hi @hclj37 !

This is expected, not a bug.c.env is not designed for handling .env values. It’s only for variables in Cloudflare Workers.

But, we might consider adding the possibility to look at the value of .env, i.e., the value of process.env for Bun or Node.js.

@knileshh

Edit: Sorry for the inconvenience guys, but it’s working it wasn’t working on the local dev environment, when I deployed and checked the keys were being shown. Thankyou.

Yeah. We have to write it into .dev.var for local development: https://developers.cloudflare.com/workers/configuration/secrets/#secrets-in-development

Q. Is there a way to use the .env without using the c ??

If you are using Cloudflare Workers, the answer is no. To get the variables, “Env” variables are needed in c.

I guess wrangler.toml use for local development, so you can gitignore it, and put your env varibales like this wrangler secret put SECRET_KEY

Yes loading from process.env should allow us to get env vars for both Bun and Node environments. I guess Deno.env.get() (or whatever the API is) could also be included, although I think Deno Deploy provides a way to load the env vars already right?

But, I think if we could get the values from process.env or Bun.env we wouldn’t have to force it to be implemented. In the case of Cloudflare, it is not possible to get environment variables from the global, so this is the only way to do so.

@ThatOneBro

What do you think about it?