#TypeError: Cannot read properties of undefined (reading 'VITE_YOUTUBE_API_KEY')

1 messages · Page 1 of 1 (latest)

hollow shoal
#

Hi! I'm getting this error when trying to read an Env Variable. My .env is in the root directory, the variable is prefixed by VITE, and I'm trying to import it doing

const apiKey = import.meta.env.VITE_YOUTUBE_API_KEY;

I've scoured the web, but most of the answers were "use VITE prefix" or "use import.meta.env", so no luck there. I also tried using loadEnv like this https://vitejs.dev/config/#using-environment-variables-in-config, but I get "process is not defined" at js const env = loadEnv(mode, process.cwd(), '')
Any help would be appreciated!

unborn yarrow
#

Are you trying to get the variable in the vite config? If so, then loadEnv is probably on the right track. That process isn’t defined is a different problem. If you’re using typescrlpt, the you probably need to add the @types/node package so that node globals are available. Alternatively, just replace the process.cwd() call with '.’, which should amount to the same thing.

hollow shoal
# unborn yarrow Are you trying to get the variable in the vite config? If so, then loadEnv is pr...

I'm not using Typescript, and I'm not trying to get the variable in the Vite config. I'm just trying to use the env variables "normally": define the variables in the .env and then import them when required in the backend files. But when I import them I get that error. As I understand it, all I had to do was define the variables with the prefix VITE_ and then import them using import.meta.env, but I'm getting that error when I do that.

unborn yarrow
#

No idea then. Is your repo public? Or can you create a public repo that demonstrates the problem?

high plover
hollow shoal
high plover
#

i never had this issue. so best help is to google

hollow shoal
high plover
#

what OS ?

hollow shoal
#

Windows 10

high plover
#

ok

#

any firewalls? any other program that might be interfering:?