Hermes Alves
Hermes Alves10h ago

Why isn't the API_PORT variable being loaded? Is there something I'm missing?

I'm trying to load an environment variable API_PORT using Deno and the @std/dotenv module, but it’s not being loaded correctly. The variable is undefined when I try to access it with Deno.env.get('API_PORT'). Here’s the code:
import { Hono } from '@hono/hono';
import { load } from "@std/dotenv"
const envVar = await load();

const app = new Hono();
console.log('PORT? ', Deno.env.get('API_PORT'), envVar);
app.get('/', (c) => c.text('Hello Deno!'));
const port = parseInt(Deno.env.get('API_PORT') || '8080');

Deno.serve({ port: port}, app.fetch);
import { Hono } from '@hono/hono';
import { load } from "@std/dotenv"
const envVar = await load();

const app = new Hono();
console.log('PORT? ', Deno.env.get('API_PORT'), envVar);
app.get('/', (c) => c.text('Hello Deno!'));
const port = parseInt(Deno.env.get('API_PORT') || '8080');

Deno.serve({ port: port}, app.fetch);
Why isn't the API_PORT variable being loaded? Is there something I'm missing?
9 Replies
bartlomieju
bartlomieju10h ago
How does your env file look like?
SebG
SebG10h ago
I think you have to specify to export to the Deno env
await load({export: true})
await load({export: true})
Hermes Alves
Hermes Alves9h ago
The .env file is located in the root of the project, alongside the deno.json. The api folder contains the Hono-based API project. Here's the structure:
/.env
/deno.json
/api
├── app.ts
├── controllers
├── models
├── routes
├── services
/.env
/deno.json
/api
├── app.ts
├── controllers
├── models
├── routes
├── services
Any idea why the API_PORT isn't loading? Didn´t work too.
# API
JWT_SECRET=$omeRand0mStr1ng
API_PORT=3737
# API
JWT_SECRET=$omeRand0mStr1ng
API_PORT=3737
bartlomieju
bartlomieju9h ago
load - @std/dotenv - JSR
@std/dotenv on JSR: UNSTABLE: Parsing and loading environment variables from a .env file
bartlomieju
bartlomieju9h ago
You need to specify { export: true } And you might have to specify the path to the .env file, but you need to double check that
Hermes Alves
Hermes Alves9h ago
No description
Hermes Alves
Hermes Alves9h ago
Yes, with a specify the envPath this work´s: const envVar = await load({envPath: "../.env", export: true}) I'm new to Deno and I have a question regarding environment variables. For development, I need to specify the path to my .env file using dotenv to load the environment variables. However, when running in production (inside a Docker container), I assume I don't need to use dotenv since Docker can handle environment variables directly. Is my understanding correct? Do I need to manage .env files differently for development and production?
bartlomieju
bartlomieju9h ago
You can use --env flag built-into Deno to load this file Then you can have exact same command locally as in production I don't know if Docker supports .env files
Hermes Alves
Hermes Alves7h ago
Thanks for your attentions guys