env variables are empty - Hono
Hello can you help me with some small example how to work locally with env variables?
I tried c.env.API_KEY
i tried env(c)
i tried env(c, "workerd")
each combo with .dev.vars and wrangler.toml vars
Every time when wranger dev it says
(wrangler.toml)
Your worker has access to the following bindings:
- Vars:
- COOKIE_SECRET: "secret"
- AUTH_URL: "https://auth-dev.service.com/"
or
(.dev.vars)
Using vars defined in .dev.vars
Your worker has access to the following bindings:
- Vars:
- COOKIE_SECRET: "(hidden)"
- AUTH_URL: "(hidden)"
using wrangler dev, wrangler dev --local or i turned on remote mode. Same result everytime which is empty object / undefined variable when i console log it.
Any tips? checked github and hono docs. Seems like I am missing something really trivial. How are you using hono locally with env vars? Seems weird that it says Your worker has access to the following bindings but there is nothing :/
17 Replies
Have you tried just accessing the variables directly? IIRC the env object isn’t loggable
my code that worked before with hard coded values is crashing because of null/undefined so my guess is that its not working but i will check and let you know
import { OpenAPIHono, createRoute, z } from '@hono/zod-openapi'
import type { Handler } from "hono"
import { env } from "hono/adapter"
const app = new OpenAPIHono()
const route = createRoute({
method: 'get',
path: '/test',
responses: {
200: {
content: {
'application/json': {
schema: z.string(),
},
},
description: 'Test',
},
},
tags: ['test']
})
const handler:Handler = async (c) => {
const { auth } = env(c)
return c.json(
Value is: ${auth})
}
export default app.openapi(route, handler)
is this what you meant?
I'm getting a value:
Looks like you need to access
AUTH_URL
or COOKIE_SECRET
Since those are the env vars you have defined.
Or rename one of them to auth
oops my bad, quickly created test route (facepalm)
do you use OpenAPIHono too?
maybe i should try with normal Hono instance
Here's what I have:
ofc, hmm. what command you use to run server?
is there requirement to do something on my cloudflare account?
wrangler dev -j
The -j
is just because I use wrangler.json
Here's my wrangler.json
: did you define secret also in this file?
No, in
.dev.vars
: damn, nothing works. I will try to create minimal example and let you know. Thank you for your assistance!!
Want me to push mine to Gh?
No need i got it 😦 so stuipid
My index.ts
import { handle } from 'hono/vercel'
import app from '../app.js'
export const config = {
runtime: 'edge'
}
export default {
fetch: handle(app)
};
when i moved to Cloudflare from vercel i didnt make properly
Oh, you are using Next?
everything worked even on this setup except env vars, so when i changed it to this it works now
import app from '../app.js'
export const config = {
runtime: 'edge'
}
export default {
fetch: app.fetch
};
no, i was using vercel for backend
then i got 1 issue with library and moved to cloudflare
thank you very much with help