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
Hello, I’m Allie!
Have you tried just accessing the variables directly? IIRC the env object isn’t loggable
Drogbata
DrogbataOP9mo ago
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)
Drogbata
DrogbataOP9mo ago
No description
Drogbata
DrogbataOP9mo ago
is this what you meant?
Hello, I’m Allie!
I'm getting a value:
"Value is: DoubleTreeSecretCookieFormula"
"Value is: DoubleTreeSecretCookieFormula"
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
Drogbata
DrogbataOP9mo ago
oops my bad, quickly created test route (facepalm)
No description
No description
No description
Drogbata
DrogbataOP9mo ago
do you use OpenAPIHono too? maybe i should try with normal Hono instance
Hello, I’m Allie!
Here's what I have:
Drogbata
DrogbataOP9mo ago
ofc, hmm. what command you use to run server? is there requirement to do something on my cloudflare account?
Hello, I’m Allie!
wrangler dev -j The -j is just because I use wrangler.json Here's my wrangler.json:
{
"name": "hono-openapi-env",
"compatibility_date": "2024-04-02",
"main": "src/index.ts"
}
{
"name": "hono-openapi-env",
"compatibility_date": "2024-04-02",
"main": "src/index.ts"
}
Drogbata
DrogbataOP9mo ago
did you define secret also in this file?
Hello, I’m Allie!
No, in .dev.vars:
COOKIE_SECRET=DoubleTreeSecretCookieFormula
AUTH_URL=https://auth.dhs.gov
COOKIE_SECRET=DoubleTreeSecretCookieFormula
AUTH_URL=https://auth.dhs.gov
Drogbata
DrogbataOP9mo ago
damn, nothing works. I will try to create minimal example and let you know. Thank you for your assistance!!
Hello, I’m Allie!
Want me to push mine to Gh?
Drogbata
DrogbataOP9mo ago
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
Hello, I’m Allie!
Oh, you are using Next?
Drogbata
DrogbataOP9mo ago
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
Want results from more Discord servers?
Add your server