Unable to set RSA private key as config var

I was able to solve my issue using a comment suggestion I found here.

So on the Heroku dashboard for the app, I set the PRIVATE_KEY value to be double quoted:

private key screenshot

I then changed

private_key: process.env.PRIVATE_KEY

to

private_key: JSON.parse(process.env.PRIVATE_KEY)

and finally redeployed via git push heroku master and the app started up fine.


2018-10-27: The comments from the link above are gone now. But essentially the commenter had mentioned that the string is effectively a JSON string which needs to be parsed back into a String.

2019-08-01: The domain originally linked is no longer valid. So I have replaced the original link with one from the Wayback Machine.


Your key inside your .env must be wrapped inside a single and double qoutes in the same time like this
'"[PRIVATE_KEY]"' which indeed the same format you will get from heroku when you call this command
$ heroku config:get PRIVATE_KEY -s >> .env
One more thing to add to JSON.parse in case it stills gets you an error which you will find in here:
Node.js -Firebase Service Account Private Key won't parse

So after you came back from that page, the final code line will look like this: JSON.parse(process.env.PRIVATE_KEY).replace(/\\n/g, '\n')

Tags:

Heroku