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
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

and where is your serviceaccount.json file located?
Seppala Siberian SleddogOP
in the root directory
@Seppala Siberian Sleddog in the root directory
try reading the file, while also constructing the path using process.cwd to determine the project's root directory
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?
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
@alfonsus ardani no, umm when do you plan to read the file?
Seppala Siberian SleddogOP
it's a request time for sending the credentials to Google API
its a json file, shouldn't you be able to import it directly?
Seppala Siberian SleddogOP
the SDK access it from this environment variable GOOGLE_APPLICATION_CREDENTIALS
oh...
Is there any other way for the SDK to read the json file?
i found a way to authentcate using an API key but it would be nice to know how to use serviceaccout.json
two approaches
Answer
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