If I have an env file .env.development in the root directory, this works:
// .env.development
PUBLIC_FOO=foo
// foo.svelte
import { PUBLIC_FOO } from '$env/static/public';
...
console.log(PUBLIC_FOO); // works
However, if I have a custom env directory, it does not. But import.meta.env does work (for VITE_ properties as mentioned in the vite docs, but not PUBLIC_)
// env/.env.development
PUBLIC_FOO=foo
VITE_FOO=foo
// vite.config.ts
export default defineConfig({
plugins: [sveltekit()],
envDir: 'env',
test: {
include: ['src/**/*.{test,spec}.{js,ts}'],
}
});
// foo.svelte
import { PUBLIC_FOO } from '$env/static/public';
...
console.log(PUBLIC_FOO); // does not work
console.log(import.meta.env.PUBLIC_FOO) // does not work
console.log(import.meta.env.VITE_FOO) // works
Update: I tried this too. Did not help:
export default defineConfig(({ mode }) => {
loadEnv(mode, 'env', '');
return {
plugins: [sveltekit()],
envDir: 'env',
kit: {
env: {
dir: 'env',
},
},
test: {
include: ['src/**/*.{test,spec}.{js,ts}'],
},
};
});