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

Environment variables registered on the Deno Deploy dashboard were 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 worked, but the deployment failed with: error: Uncaught (in promise) LibsqlError: URLINVALID: The URL is not in a valid format — because Deno.env.get("tursourl") returned undefined at module evaluation time.

On Deno Deploy, dashboard environment variables 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. The reporter confirmed that wrapping the createClient({ url: Deno.env.get("tursourl"), auth: Deno.env.get("...") }) call inside a function made the deployment work. As a general rule for Deploy: never build clients or read env vars at module top level; do it lazily inside the request handler. Source: https://github.com/denoland/deployfeedback/issues/574

Source: https://github.com/denoland/deploy_feedback/issues/574