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
what's your actual issue?
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.why not use
process.env?
or Deno.env, for that matter
you don't have to use $env/* stuff, it's just a sveltekit conventionI can't use it in SvelteKit for some reason. it fails to load Deno.env. Here's reduce code example
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 finewhat's your command to run sveltekit?
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.
if you do
DATABASE_URL=test vite dev, it sees nothing? are you sure?if I do it like that it works but is this good practice?
you should be able to pass an env flag, too
https://docs.deno.com/runtime/reference/env_variables/
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'
could you show me your package.json? or a redacted version just showing the scripts
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):
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 flexibleThank 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.
sure, no worries! if you ever want to try out my framework, drop me a message, otherwise much luck 🙂