Running scripts that have access to Kit stores (like $env)
See original GitHub issueDescribe the problem
I really like the $env
store, so I migrated the whole fullstack app to access env vars only through $env
. The problem now is that I have some backend node.js scripts (database setup and migrations and such) which need to import some model code from my app. The model code imports $env
, and so I can’t import those into my script because that prevents me from running my script, b/c when I try to run my script with node my-script.js
I get an error like this
Error [ERR_MODULE_NOT_FOUND]: Cannot find package '$env'
…because a dep of my dep is $env
. I need my script to be run via SvelteKit somehow in order to resolve the $env
alias.
Describe the proposed solution
Something like this would be nice…
svelte-kit run my-script.js
That would allow devs to run scripts which can access the $env
store.
Alternatives considered
The only alternative I can think of would be to go back to using vanilla process.env
. This has the disadvantage of needing to manually load via something like dotenv
, as well as losing the protections of private vs public env vars, and losing the ability to statically build env values into the bundle.
Importance
nice to have
Additional Information
No response
Issue Analytics
- State:
- Created a year ago
- Comments:6 (3 by maintainers)
@bluwy Awesome suggestion! It turns out that straight up
npx vite-node my-script.js
works out of the box. Now I can use$env
everywhere 🙌I’ve run into a similar issue - this time with Vitest. I’m trying to run tests on a script that imports env vars using SvelteKit’s
$env
.I really want to use
$env
but I’ve had to switch back toimport.meta.env...
to circumvent this.