honojs / honox

HonoX - Hono based meta framework
https://hono.dev
MIT License
1.35k stars 38 forks source link

ENV in Honox #79

Open divofred opened 6 months ago

divofred commented 6 months ago

What is the feature you are proposing?

I looked through the documentation and I did not see anywhere documenting how to use ENVs. I checked VITE and saw that I can use ENV by creating an .env file and adding the VITE_ prefix to the env name. Sample VITE_HELLO=WORLD. Is this the best way or is there another?

bruceharrison1984 commented 6 months ago

Since HonoX is built on Vite, dotfiles should work fine. They can be accessed via the Context object within any route.

export default createRoute(async (c) => {
  console.log(c.env.DEBUG)
  return c.html(<html>{c.env.DEBUG}</html>);
});
divofred commented 6 months ago

@bruceharrison1984 It did not seem to work without the adding the VITE_ prefix. What am I doing wrong?

niicojs commented 6 months ago

in my case, I'm doing it in vite config. not ideal but good enough for my use case

export default defineConfig(async ({ command, mode }) => {
  if (command === 'serve') {
    await import('dotenv/config');
  }
  ...
terenced commented 4 months ago

@divofred If I am not mistaken, the VITE_ prefix is my design. Vite does this to prevent accidentally leaking env variables. https://vitejs.dev/guide/env-and-mode#env-files

divofred commented 4 months ago

@divofred If I am not mistaken, the VITE_ prefix is my design. Vite does this to prevent accidentally leaking env variables. https://vitejs.dev/guide/env-and-mode#env-files

Alright, thanks. @terenced