derschiw
derschiw

Reputation: 350

Using $env with SvelteKit and Docker

SvelteKit provides a good and easy way to use ENV variables:

https://learn.svelte.dev/tutorial/env-static-public

Im using Docker in a CI pipeline to create images. However when running RUN npm run build in the Dockerfile this leads to the TypeScript error described here: Sveltekit + Typescript: Environment variables build error.

The solution provided in the related answer (i.e. running svelte-kit sync) is not possible, since in the CI pipeline there is no ENV file during build. Therefore it won't find the variable and the build fails with the error above.

Is there another solution to do this?

Upvotes: 6

Views: 1116

Answers (3)

codoor
codoor

Reputation: 11

I have a RUN cp ./env.example ./.env to define all the required variables as empty values in order to pass the build. I should probably also add a runtime check that loops over the .env.example keys to make sure they are all there since I am bypassing the SvelteKit check.

Upvotes: 1

oomer
oomer

Reputation: 169

This tutorial https://learn.svelte.dev/tutorial/env-static-private accompanying the docs mentions

Environment variables in process.env are also available via $env/static/private.

It seems it applies to public env vars, too i-e if you add a public env var such as PUBLIC_KEY to process.env, it is accessible via

import { PUBLIC_KEY } from '$env/static/public';

Upvotes: 0

derschiw
derschiw

Reputation: 350

I ended up using dynamic ENV variables https://learn.svelte.dev/tutorial/env-dynamic-public . This is not my preferred choice but at least i can build now..

Upvotes: 1

Related Questions