Next.js Discord

Discord Forum

How to reference a file as an environment variable in Vercel?

Answered
Seppala Siberian Sleddog posted this in #help-forum
Open in Discord
Avatar
Seppala Siberian SleddogOP
I'm using Google Cloud packages so they require the environment variable to be stored as a file so i have downloaded the file on my local machene and referenced it like so
GOOGLE_APPLICATION_CREDENTIALS="serviceaccount.json"

and i have the file in the root of my project but i can't access it in Vercel i get this error
Error: The file at "serviceaccount.json" does not exist, or it is not a file. ENOENT: no such file or directory, lstat '/var/task/"serviceaccount.json"'
Answered by linesofcode
two approaches
View full answer

18 Replies

Avatar
Alfonsus Ardani
and where is your serviceaccount.json file located?
Avatar
Seppala Siberian SleddogOP
in the root directory
Avatar
Alfonsus Ardani
try reading the file, while also constructing the path using process.cwd to determine the project's root directory
Avatar
Seppala Siberian SleddogOP
reading files isn't allowed in Vercel, i have tried it, and the file should be specified in the environment variable not in the code, does process.cwd works in .env files?
Avatar
Alfonsus Ardani
no, umm when do you plan to read the file?
if its at build time, its fine actually
if its at request time, im not sure
Avatar
Seppala Siberian SleddogOP
it's a request time for sending the credentials to Google API
Avatar
Alfonsus Ardani
its a json file, shouldn't you be able to import it directly?
Avatar
Seppala Siberian SleddogOP
the SDK access it from this environment variable GOOGLE_APPLICATION_CREDENTIALS
Avatar
Alfonsus Ardani
oh...
Is there any other way for the SDK to read the json file?
Avatar
Seppala Siberian SleddogOP
i found a way to authentcate using an API key but it would be nice to know how to use serviceaccout.json
Avatar
linesofcode
two approaches
Answer
Avatar
linesofcode
1) store the serviceaccount json as a giant string in an env variable
2) if your env variables are exceeding vercel limit (4kb), encrypt the service account json file and store it in your codebase. During runtime decrypt it
Avatar
Seppala Siberian SleddogOP
okay thanks