Julien-R44 / vite-plugin-validate-env

✅ Vite plugin for validating your environment variables
MIT License
164 stars 6 forks source link

[Bug] Plugin always reports that env variables are not present, but they are #7

Closed CerealKiller97 closed 1 year ago

CerealKiller97 commented 1 year ago

Hello,

I've just seen this amazing plugin, and I wanted to put it in my SvelteKit project. I have read the docs a couple of times, and I am still getting the same issues, it says that my variables are not present but they are.

This is how my .env file looks like:

INSTAGRAM_CLIENT_ID=whatever
INSTAGRAM_CLIENT_SECRET=whatever

PUBLIC_SUPABASE_URL=whatever
PUBLIC_SUPABASE_ANON_KEY=whatever

I have extracted validation logic to env.ts file:

import { defineConfig, Schema } from '@julr/vite-plugin-validate-env';

export default defineConfig({
    INSTAGRAM_CLIENT_ID: Schema.string(),
    INSTAGRAM_CLIENT_SECRET: Schema.string(),
    PUBLIC_SUPABASE_URL: Schema.string(),
    PUBLIC_SUPABASE_ANON_KEY: Schema.string()
});
Screenshot 2023-03-12 at 13 14 02
Julien-R44 commented 1 year ago

I'm not familiar with SvelteKit at all but I think they use their own method to load environment variables so I guess the plugin is not compatible.

Let's say I replace your env.ts with :

import { defineConfig, Schema } from "@julr/vite-plugin-validate-env";

export default defineConfig({
  VITE_INSTAGRAM_CLIENT_ID: Schema.string(),
  VITE_INSTAGRAM_CLIENT_SECRET: Schema.string(),
  VITE_PUBLIC_SUPABASE_URL: Schema.string(),
  VITE_PUBLIC_SUPABASE_ANON_KEY: Schema.string(),
});

And add the VITE_ prefix to all your variable in your .env file This should work.

However, it is likely that these variables are also injected into the client code. Which you don't want because some of them are sensitive data. Maybe you can try to verify

CerealKiller97 commented 1 year ago

@Julien-R44 I've tried this way, didn't work, will explore more, I will go through SvelteKit plugin for Vite, will notify you If I find something interesting.

Julien-R44 commented 1 year ago

Tried on my side and it worked Feel to share a repo with the issue and I can take a look

CerealKiller97 commented 1 year ago

@Julien-R44 I've tried it and it's working. Thank you once again!