Environment variables containing newlines in Node?

后端 未结 8 460
再見小時候
再見小時候 2021-02-02 06:00

I\'m attempting to load an RSA private key into my nodejs application using environment variables, but the newlines seem to be being auto-escaped.

For the following, ass

8条回答
  •  再見小時候
    2021-02-02 06:34

    If you're using dotenv: We solved it this way, with newlines and JSON.parse (this allows any backslash escaped chars within the string, not just \n):

    in .env:

    MY_KEY='-----BEGIN CERTIFICATE-----\nabcde...'
    

    in server.ts:

    myKey = JSON.parse(`"${process.env.MY_KEY}"`), // convert special chars
    

    See thread: https://github.com/motdotla/dotenv/issues/218

提交回复
热议问题