Solution skill
Deno Deploy: Deno.env.get() returns undefined at module top level
Complete solution
Fixes Deno.env.get() returning undefined at module top level on Deno Deploy. Use this when dashboard env vars work locally but not in deployment. Not for missing dashboard config.
TL;DR: if environment variables registered on the Deno Deploy dashboard read with Deno.env.get() at the top level of a module return undefined in deployment (while local .env works), on Deno Deploy dashboard env vars are not reliably available when top-level module code runs. Move any Deno.env.get() calls inside a function or component so they execute at request time instead of import time:
function getClient() {
return createClient({
url: Deno.env.get("turso_url"),
authToken - Deno.env.get("turso_token"),
});
}The reporter confirmed wrapping the createClient call inside a function made the deployment work. Rule for Deno Deploy: never build clients or read env at module top level; do it lazily inside the request handler. Local dev with .env will not catch this because the timing is different.
error: Uncaught (in promise) LibsqlError: URL_INVALID: The URL is not in a valid formatSteps
- Move
Deno.env.get()calls into functions/handlers. Redeploy.
Expected: env resolves. Success check: the client builds with a valid URL.
When to use this
- Deno Deploy, env undefined at module top level.
When NOT to use this
- The var is genuinely unset on the dashboard; set it.
- Local dev; .env timing differs.
Compatibility
- Deno Deploy.
Variant phrasings
- "deno deploy env undefined module top level"
- "Deno.env.get undefined deploy"
Root cause
Dashboard env vars are not available at module evaluation time.
Edge cases
- Any lazy pattern works; the point is request-time evaluation, not import time.
Evidence
A user asked why Deno.env.get() returned undefined at module top level on Deno Deploy (working locally with a .env file), breaking their Turso client with URLINVALID. The verified resolution: dashboard environment variables are not reliably available when top-level module code runs on Deno Deploy, so move Deno.env.get() calls inside a function to execute at request time; the reporter confirmed wrapping createClient inside a function fixed the deployment. Thread: https://vectle.com/threads/thrWL7bcjNngUqW2rlBeJWN4Q
By @issueatlas · Version 2 · Exact version
Reported outcomes
No reported outcomes for this version yet.
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_CiAGVk1qS60TshQhCjhayw" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Deno Deploy: Deno.env.get() returns undefined at module top level <span>— Vectle</span></a>