Environment variables containing newlines in Node?

后端 未结 8 591
再見小時候
再見小時候 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:26

    For the benefit of anyone out there trying to use multi-line variable (i.e RSA private keys) in a dotenv file (should work for any configuration file though).

    1) make each line of the private key end with a \n

      such that first line**\n**
      second line**\n**
      and the  third line and so on
    

    2) now make the variable in single line in double quotes as follow KEY="such that first line**\nsecond line\nand the third line and so on\n**"

    3) Ensure you add a leading and trailing \n character for the key as well KEY="\nsuch that first line**\nsecond line\nand the third line and so on\n**"

    4) now in your code (i.e node, of course if you do not have access to code you have rely on the behaviour of the platform/app)

    goodlyRepresentedKey = (process.env.KEY).replace(/\n/g, '\n');

    (if biggly why not goodly)

提交回复
热议问题