jose
jose13mo 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
iuioiua13mo 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
joseOP13mo ago
Oh sorry, I writed it wrongly on the message, actually the file have = instead of : Still importing dotenv in main.ts?
iuioiua
iuioiua13mo 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
joseOP13mo ago
Thankss somehow, still getting undefined
iuioiua
iuioiua13mo ago
Are you able to share the repo?
jose
joseOP13mo ago
yes, ill put it in github just a sec
jose
joseOP13mo 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 Sampaio13mo ago
youre trying to get TEST_TOKEN instead of FOO
jose
joseOP13mo ago
omg 😭
iuioiua
iuioiua13mo ago
LOL! Yes, didn’t see that. I had a feeling it was a silly typo lol
Antonio Sampaio
Antonio Sampaio13mo ago
and.... you're trying to log test instead of testgi
jose
joseOP13mo ago
oh that i must've writed when i was configuring git to publish it finally it worked lmaoo
Antonio Sampaio
Antonio Sampaio13mo ago
<:deno_it_works:1181937887458111508> <:deno_thumbs_up:1181937842390319184>
jose
joseOP13mo ago
thank you sm

Did you find this page helpful?