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 :/
HIA
Hello, I’m Alastair!14d ago
Have you tried just accessing the variables directly? IIRC the env object isn’t loggable
D
Drogbata14d 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)
D
Drogbata14d ago
No description
D
Drogbata14d ago
is this what you meant?
HIA
Hello, I’m Alastair!14d ago
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
D
Drogbata14d ago
oops my bad, quickly created test route (facepalm)
No description
No description
No description
D
Drogbata14d ago
do you use OpenAPIHono too? maybe i should try with normal Hono instance
HIA
Hello, I’m Alastair!14d ago
Here's what I have:
D
Drogbata14d ago
ofc, hmm. what command you use to run server? is there requirement to do something on my cloudflare account?
HIA
Hello, I’m Alastair!14d ago
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"
}
D
Drogbata14d ago
did you define secret also in this file?
HIA
Hello, I’m Alastair!14d ago
No, in .dev.vars:
COOKIE_SECRET=DoubleTreeSecretCookieFormula
AUTH_URL=https://auth.dhs.gov
COOKIE_SECRET=DoubleTreeSecretCookieFormula
AUTH_URL=https://auth.dhs.gov
D
Drogbata14d ago
damn, nothing works. I will try to create minimal example and let you know. Thank you for your assistance!!
HIA
Hello, I’m Alastair!14d ago
Want me to push mine to Gh?
D
Drogbata14d 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
HIA
Hello, I’m Alastair!14d ago
Oh, you are using Next?
D
Drogbata14d 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
More Posts
I will check that out. Thank you! Do youI will check that out. Thank you! Do you mind me asking what tech stack you use? Do you try to use a522 ErrorHi all, I tried updating the CNAME from '@' to 'www.' so I can so more dynamic stuff with multi-repwrangler preview output is not the same as liveHello all, finally I got my astro project buildable. Using `wrangler pages dev ./dist` my website lWhat headers come back?I have one question. If I use Azure and I move to ai gateway will I still be able to receive the heaMultiple Set-Cookie headers folded into one headerIn the documentation (https://developers.cloudflare.com/workers/runtime-apis/headers/), it says thatCloudflare Tunnel routing through wrong locationsI have a tunnel that I use as an alternative to opening ports on my local network, however, there isRegister DomainHi. I cant pay for new .com domain. I can't figure out what could be the problem. The bank side saysCloudflare icons and stencils for diagramming.Do we have an open source repository with Cloudflare-specific icons?"Pro" plan, but can't see the Caching page.Hi there, I've subscribed to the "Pro" plan, but it seems I don't have access to the "Caching - Ovecan you provide an url for mp3?can you provide an url for mp3?Downloading data before building pages (SSG/SSR)I have the use case that I'm experimenting building a Cloudflare Pages project using Astro. It willJust the domain is neededJust the domain is neededRequest failed with status code 408 (Request Timeout)Failed upload sourcemap for [dist/assets/SecondaryButton-e.js.map]: Request failed with status code opened a PR to allow native web_sysopened a PR to allow native web_sys request, without having to ditch the event macro: https://githubVPN tunnel warp 1.1.1.1 problemI need help ! I can't activate my VPN. When I want to activate it, the following words appear: Your how to setup custom 404 on static site(vanilla, no framework)?i want to deploy a static site using regular html, css and js, built with vite(so not totally vanillnode:path fails even with compat flagDNS Proxy for media server?I know previously the rule of thumb was disable DNS proxy when using cloudflare due to ToS breach buOrdering of zaraz.track requestsHey team! We're using Zaraz to pipe our analytics events from our browser client to GA4. We're calliFetching From D1 using WorkersHow Do I Create an endpoint using Cloudflare D1 and workers that returns the entire table as JSON wh