Skip to content

Accessing env variables in standalone scripts #14891

@petdomaa100

Description

@petdomaa100

Describe the problem

Unless I'm missing something, there is currently no way to access $env/static/private outside of the Vite context, the file simply doesn't exist.

My database driver is defined in a file that's imported by SvelteKit code and a DB migration script. The latter is executed as a standalone CLI script using bun.

In the standalone execution context import.meta.env is defined but $env/static/private doesn't exist, and in the SvelteKit execution context it's the opposite. Is there a recommended way to deal with this?

My current solution:

const SOME_ENV_VAR = import.meta.env.SOME_ENV_VAR ?? (await import('$env/static/private')).SOME_ENV_VAR;

Seems quite hacky and relies on top-level await but it works fine for my usecase.

Describe the proposed solution

Ideally, there would be a command in the svelte-kit CLI that runs a file in the Vite context.

Alternatives considered

No response

Importance

nice to have

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions