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)
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 ofprocess.env
for Bun or Node.js.@knileshh
Yeah. We have to write it into
.dev.var
for local development: https://developers.cloudflare.com/workers/configuration/secrets/#secrets-in-developmentIf 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 guessDeno.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
orBun.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?
Hi @muhaimincs
Now, we can use the
env()
: https://hono.dev/helpers/adapter#env