I'm using firebase in my node js. application, and I want to store its serviceAccountKey.json file in a process.env variable.
Something like this in my dotenv (.env) file
SERVICE_ACCOUNT_KEY={
"type": "anything",
"project_id": "anything",
"private_key_id": "anything",
"private_key": "anything",
"client_email": "anything",
"client_id": "anything",
"auth_uri": "anything",
"token_uri": "anything",
"auth_provider_x509_cert_url": "anything",
"client_x509_cert_url": "anything"
}
But when I do this, It says
Service account must be an object.
Please help me storing this object in process.env variable.
CodePudding user response:
You could try to store the object as a string and parse it as JSON in your code.
.env :
MY_VAR='{"a":"valueA","b":"valueB"}'
Then in the code
app.js :
let object = JSON.parse(process.env.MY_VAR);
EDIT ( thanks @Luca Galasso ) Reformed a correct JSON string.
CodePudding user response:
When storing a variable in the process.env
, it will be automatically converted as a string.
Given that, if you'd like to set a variable in the process.env
, either you pass a proper string object-like while running your script:
SERVICE_ACCOUNT_KEY='{"type":"anything"}' node script.js
or you cast the object to a string in your script.js file like:
process.env.SERVICE_ACCOUNT_KEY = JSON.parse(SERVICE_ACCOUNT_KEY)
In both cases, while reading your variable from the process.env, you should convert it to an object:
SERVICE_ACCOUNT_KEY = JSON.parse(process.env.SERVICE_ACCOUNT_KEY)
Since the error you wrote says "... must be an object.", most probably the missing piece is the last step (parsing from string to object).