question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Running scripts that have access to Kit stores (like $env)

See original GitHub issue

Describe 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:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
acarl005commented, Aug 15, 2022

@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 🙌

1reaction
DoisKohcommented, Aug 16, 2022

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 to import.meta.env... to circumvent this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Modules • Docs • SvelteKit
SvelteKit analyses your app during the build step by running it. ... we don't need to worry about this, and stores can be...
Read more >
Environments - AWS Cloud Development Kit (AWS CDK) v2
Each Stack instance in your AWS CDK app is explicitly or implicitly associated with an environment ( env ). An environment is the...
Read more >
Using a Server List to Control PowerShell Scripts - Simple Talk
To access your environment variables, you need to tell PowerShell ... Up until now, all the scripts you've run have used built-in objects....
Read more >
Using Python for scripting and automation | Microsoft Learn
This article will cover setting up your environment to use some of the helpful libraries in Python that can automate tasks across platforms, ......
Read more >
Types of shells supported by GitLab Runner
GitLab Runner implements a few shell script generators that allow executing builds on different systems. Overview. The shell scripts contain commands to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found