babayaga
babayaga•2mo ago

Help use enviroment variable for both Deno and SvelteKit

Changed post since I solved the original issue. Now I'm having a very annoying issue with trying to use an enviroment variable that works for both Deno and SveltKit.
15 Replies
blue
blue•2mo ago
what's your actual issue?
babayaga
babayagaOP•2mo ago
I'm trying to seed a database with a Deno task (basically a file that doesn't run in the lifetime of the SvelteKit app) but the env variable that SvelteKit uses by default $env/dynamic/private isn't understood in the Deno runtime when I run that task if that makes sense. It tries to import it as a package. So now I'm trying to use the @std/dotenv of Deno but now SvelteKit can't find the module. I've tried installing the package with npx jsr but it fails for some reason.
blue
blue•2mo ago
why not use process.env? or Deno.env, for that matter you don't have to use $env/* stuff, it's just a sveltekit convention
babayaga
babayagaOP•2mo ago
I can't use it in SvelteKit for some reason. it fails to load Deno.env. Here's reduce code example
//index.ts
import { drizzle } from 'drizzle-orm/node-postgres';
import pg from 'pg';
import * as schema from './schema.ts';
import { project as projectSchema } from './schema.ts';
//missing import that makes Deno env variable work

if (!Deno.env.get('DATABASE_URL')) throw new Error('DATABASE_URL is not set');

const { Pool } = pg;

export const db = drizzle({
client: new Pool({
connectionString: Deno.env.get('DATABASE_URL')
}),
schema: { projectSchema }
});

export async function getProjects() {
const rows = await db.select().from(schema.project);
return rows;
}


//seed.ts
import { db } from './index.ts';
import * as schema from './schema.ts';

async function seed() {
await db.delete(schema.cue).execute();
await db.delete(schema.project).execute();

const projects = [
{
title: 'Film Project A',
total_cues: 5,
total_time: '00:12:45:10',
kind: 'Feature',
framerate: '23.98'
},
{
title: 'Documentary B',
total_cues: 3,
total_time: '00:05:22:03',
kind: 'Documentary',
framerate: '25'
}
];

for (const project of projects) {
await db.insert(schema.project).values(project).execute();
}
}

// Run only if executed directly
if (import.meta.main) {
await seed();
}
//index.ts
import { drizzle } from 'drizzle-orm/node-postgres';
import pg from 'pg';
import * as schema from './schema.ts';
import { project as projectSchema } from './schema.ts';
//missing import that makes Deno env variable work

if (!Deno.env.get('DATABASE_URL')) throw new Error('DATABASE_URL is not set');

const { Pool } = pg;

export const db = drizzle({
client: new Pool({
connectionString: Deno.env.get('DATABASE_URL')
}),
schema: { projectSchema }
});

export async function getProjects() {
const rows = await db.select().from(schema.project);
return rows;
}


//seed.ts
import { db } from './index.ts';
import * as schema from './schema.ts';

async function seed() {
await db.delete(schema.cue).execute();
await db.delete(schema.project).execute();

const projects = [
{
title: 'Film Project A',
total_cues: 5,
total_time: '00:12:45:10',
kind: 'Feature',
framerate: '23.98'
},
{
title: 'Documentary B',
total_cues: 3,
total_time: '00:05:22:03',
kind: 'Documentary',
framerate: '25'
}
];

for (const project of projects) {
await db.insert(schema.project).values(project).execute();
}
}

// Run only if executed directly
if (import.meta.main) {
await seed();
}
if I use Deno's Deno.env in SvelteKit I get "Error: DATABASE_URL is not set". if I run deno task seed it works fine
blue
blue•2mo ago
what's your command to run sveltekit?
babayaga
babayagaOP•2mo ago
It's the default for SvelteKit Deno apps deno task dev , so I guess vite dev https://docs.deno.com/examples/svelte_tutorial/
Deno
Build a SvelteKit App
A tutorial on building SvelteKit applications with Deno. Learn how to set up a SvelteKit project, implement file-based routing, manage state with load functions, and create a full-stack TypeScript application.
blue
blue•2mo ago
if you do DATABASE_URL=test vite dev, it sees nothing? are you sure?
babayaga
babayagaOP•2mo ago
if I do it like that it works but is this good practice?
blue
blue•2mo ago
you should be able to pass an env flag, too https://docs.deno.com/runtime/reference/env_variables/
babayaga
babayagaOP•2mo ago
not sure how that works. The default task in package.json is "dev": 'vite dev'. Simple, right? When I try to override it I have to give all kinds of permissions just to pass the flags. and vite dev no longer works, I have to use ./node_modules/.bin/vite dev'
blue
blue•2mo ago
could you show me your package.json? or a redacted version just showing the scripts
babayaga
babayagaOP•2mo ago
{
"name": "*******",
"private": true,
"version": "0.0.1",
"type": "module",
"scripts": {
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"prepare": "svelte-kit sync || echo ''",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
"format": "prettier --write .",
"lint": "prettier --check . && eslint .",
"db:push": "drizzle-kit push",
"db:generate": "drizzle-kit generate",
"db:migrate": "drizzle-kit migrate",
"db:studio": "drizzle-kit studio",
"seed": "deno run --env-file --allow-env --allow-read=.env --allow-net src/lib/server/db/seed.ts"
},
"devDependencies": {
"@eslint/compat": "^1.4.0",
"@eslint/js": "^9.36.0",
"@sveltejs/adapter-auto": "^6.1.0",
"@sveltejs/kit": "^2.43.2",
"@sveltejs/vite-plugin-svelte": "^6.2.0",
"@tailwindcss/forms": "^0.5.10",
"@tailwindcss/typography": "^0.5.18",
"@tailwindcss/vite": "^4.1.13",
"@types/better-sqlite3": "^7.6.13",
"@types/node": "^24",
"eslint": "^9.36.0",
"eslint-config-prettier": "^10.1.8",
"eslint-plugin-svelte": "^3.12.4",
"globals": "^16.4.0",
"prettier": "^3.6.2",
"prettier-plugin-svelte": "^3.4.0",
"svelte": "^5.39.5",
"svelte-check": "^4.3.2",
"tailwindcss": "^4.1.13",
"typescript": "^5.9.2",
"typescript-eslint": "^8.44.1",
"vite": "^7.1.7"
},
"dependencies": {
"@tailwindcss/postcss": "^4.1.14",
"@types/pg": "^8.15.5",
"@types/smpte-timecode": "^1.2.5",
"drizzle-kit": "^0.31.5",
"drizzle-orm": "^0.44.6",
"pg": "^8.16.3",
"smpte-timecode": "^1.3.6"
}
}
{
"name": "*******",
"private": true,
"version": "0.0.1",
"type": "module",
"scripts": {
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"prepare": "svelte-kit sync || echo ''",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
"format": "prettier --write .",
"lint": "prettier --check . && eslint .",
"db:push": "drizzle-kit push",
"db:generate": "drizzle-kit generate",
"db:migrate": "drizzle-kit migrate",
"db:studio": "drizzle-kit studio",
"seed": "deno run --env-file --allow-env --allow-read=.env --allow-net src/lib/server/db/seed.ts"
},
"devDependencies": {
"@eslint/compat": "^1.4.0",
"@eslint/js": "^9.36.0",
"@sveltejs/adapter-auto": "^6.1.0",
"@sveltejs/kit": "^2.43.2",
"@sveltejs/vite-plugin-svelte": "^6.2.0",
"@tailwindcss/forms": "^0.5.10",
"@tailwindcss/typography": "^0.5.18",
"@tailwindcss/vite": "^4.1.13",
"@types/better-sqlite3": "^7.6.13",
"@types/node": "^24",
"eslint": "^9.36.0",
"eslint-config-prettier": "^10.1.8",
"eslint-plugin-svelte": "^3.12.4",
"globals": "^16.4.0",
"prettier": "^3.6.2",
"prettier-plugin-svelte": "^3.4.0",
"svelte": "^5.39.5",
"svelte-check": "^4.3.2",
"tailwindcss": "^4.1.13",
"typescript": "^5.9.2",
"typescript-eslint": "^8.44.1",
"vite": "^7.1.7"
},
"dependencies": {
"@tailwindcss/postcss": "^4.1.14",
"@types/pg": "^8.15.5",
"@types/smpte-timecode": "^1.2.5",
"drizzle-kit": "^0.31.5",
"drizzle-orm": "^0.44.6",
"pg": "^8.16.3",
"smpte-timecode": "^1.3.6"
}
}
blue
blue•2mo ago
yeah, I think the problem is the strong coupling here, not sure vite dev supports passing deno flags (or flags to the runtime). have you checked if that's possible? I'd generally recommend you to not rely on vite with deno, it's hard. the internet says you should be able to do something like (in deno.json):
{
"tasks": {
"dev": "deno run -A npm:vite"
}
}
{
"tasks": {
"dev": "deno run -A npm:vite"
}
}
otherwise it's going to be hard to run sveltekit on deno if it's not specifically optimised for that I can recommend you my own framework which would support running stuff with deno and svelte, depends if you want to stick to sveltekit or are flexible
babayaga
babayagaOP•2mo ago
Thank you that worked. Yeah I'm not sure if Deno is for me. I will keep experimenting. I'm trying to get away from npm after the recent scandal. I guess Deno helps to reduce risks a bit. Not trivial to avoid package managers nowadays.
blue
blue•2mo ago
sure, no worries! if you ever want to try out my framework, drop me a message, otherwise much luck 🙂

Did you find this page helpful?