jose
jose12mo ago

Environment Variable always returns undefined

I'm just trying to import the values from dotenv to the code, but it always returns undefined. deno version: 1.39.1 running command: deno run --allow-read --allow-env main.ts dotenv content:
FOO=123
FOO=123
main.ts content:
import "https://deno.land/std@0.208.0/dotenv/load.ts";
console.log(Deno.env.get("FOO"));
import "https://deno.land/std@0.208.0/dotenv/load.ts";
console.log(Deno.env.get("FOO"));
I also tried
import { load } from "https://deno.land/std@0.210.0/dotenv/mod.ts";

await load({
export:true,
envPath: "./.env",
allowEmptyValues: false,
});

const test = Deno.env.get("TEST_TOKEN")
console.log(test)
import { load } from "https://deno.land/std@0.210.0/dotenv/mod.ts";

await load({
export:true,
envPath: "./.env",
allowEmptyValues: false,
});

const test = Deno.env.get("TEST_TOKEN")
console.log(test)
result is always:
Task start deno run --allow-read --allow-env main.ts
undefined
Task start deno run --allow-read --allow-env main.ts
undefined
Update: also tried deno run --env --allow-env main.ts and it still is undefined.
14 Replies
iuioiua
iuioiua12mo ago
Firstly, your .env file is formatted incorrectly. It should be FOO=123. Secondly, use the —env CLI argument instead of load(). I.e. deno run —env main.ts
jose
joseOP12mo ago
Oh sorry, I writed it wrongly on the message, actually the file have = instead of : Still importing dotenv in main.ts?
iuioiua
iuioiua12mo ago
Deno Blog
Deno 1.38: HTML doc generator and HMR
Deno 1.38 ships with HTML doc output, hot module replacement, improved Node.js compatibility by allowing you to use your own node_modules folder, and more.
jose
joseOP12mo ago
Thankss somehow, still getting undefined
iuioiua
iuioiua12mo ago
Are you able to share the repo?
jose
joseOP12mo ago
yes, ill put it in github just a sec
jose
joseOP12mo ago
not a second lmao but here it is https://github.com/nogalogg/denotest
GitHub
GitHub - nogalogg/denotest
Contribute to nogalogg/denotest development by creating an account on GitHub.
Antonio Sampaio
Antonio Sampaio12mo ago
youre trying to get TEST_TOKEN instead of FOO
jose
joseOP12mo ago
omg 😭
iuioiua
iuioiua12mo ago
LOL! Yes, didn’t see that. I had a feeling it was a silly typo lol
Antonio Sampaio
Antonio Sampaio12mo ago
and.... you're trying to log test instead of testgi
jose
joseOP12mo ago
oh that i must've writed when i was configuring git to publish it finally it worked lmaoo
Antonio Sampaio
Antonio Sampaio12mo ago
<:deno_it_works:1181937887458111508> <:deno_thumbs_up:1181937842390319184>
jose
joseOP12mo ago
thank you sm