0

I am trying to deploy a sveltekit app using adapter-node using .env files.

I have a .env file with several values:

V_TEST1=TEST1
PUBLIC_V_TEST2=TEST2

I access them using import { V_TEST1 } from '$env/static/private'; and import { PUBLIC_V_TEST2 } from '$env/static/public';

In development, I have no issues. The values get read and used.

When deploying, there are 2 ways of doing this, one is to build on development side and deploy the necessary files or build server side, sending the whole code to the server and build on the server.

If I build server side, I am able to access the .env I place on the server (different that dev .env) and have no issues. But I do not want to build on the server.

If I build on development side. The environment variables seem to be hard coded into the /build and if I place a .env on the server, the file gets ignored.

On the server I am running using node --env-file=.env build as recommended on sveltkit docs https://svelte.dev/docs/kit/adapter-node#Environment-variables.

So is there a way of accessing the .env files on the server on a pre-build application?

1 Answer 1

0

You have to switch to the $env/dynamic/* modules if you want to access variables that are loaded when running the application. (Variable access is a bit different via an env object, see docs.)

Sign up to request clarification or add additional context in comments.

4 Comments

I managed to make it work using dynamic as you mentioned. But I had to define the variables using pm2 definitions, I could not make it work using the .env file. any thoughts?
Did you run the application using --env-file? With the Node adapter you need to load the variables manually either this way or using a package like dotenv (see this docs section).
I am running using pm2 arguments: ``` { name: 'myapp', cwd: './myapp', script: 'node build', node_args: '--env-file=.env', instances: 1, autorestart: true, watch: ["./myapp/build/"], env: { PORT: 9100 } }, ```
That looks like it could work if the cwd contains the file. Maybe a pm2 issue of some kind...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.