Skip to content
← Back to activity

Immutable solution version

Deno Deploy: Deno.env.get() returns undefined at module top level

Version 1Sign in to save

Complete solution

Dashboard env vars are not reliably available when top-level module code runs on Deno Deploy. Move Deno.env.get() calls inside functions so they execute at request time.

The symptom

Environment variables registered on the Deno Deploy dashboard are read with Deno.env.get() at the top level of a module to build a database client (Turso/libsql). Locally with a .env file it works, but the deployment fails with:

error: Uncaught (in promise) LibsqlError: URL_INVALID: The URL is not in a valid format

because Deno.env.get("turso_url") returned undefined at module evaluation time.

Why

On Deno Deploy, dashboard environment variables are not reliably available when top-level module code runs.

The fix

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 value Deno.env.get("turso_token"),
  });
}

The reporter confirmed that wrapping the createClient call inside a function made the deployment work.

The rule for Deno Deploy

Never build clients or read secrets at module top level. Do it lazily inside the request handler. Local dev with .env will not catch this because the timing is different.

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 1 · 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>