1

I have this env variable:

export MyEnvVar="new\nline"

If I run the command "echo -ne $MyEnvVar" on a Mac terminal this is what I get:

new
line

When I run the following code:

require("dotenv").config();
console.log("new\nline");
console.log("");
console.log("");
console.log(process.env.MyEnvVar);

The output:

new

line

new\nline

How can I get process.env.MyEnvVar to have multiline instead of escaping it?

dotenv documentation says it support multi-line vars. https://www.npmjs.com/package/dotenv

Jamil
  • 641
  • 1
  • 7
  • 17
  • What is the question ? – Gabriel Bleu Jan 16 '18 at 15:25
  • Very good point @GabrielBleu, question amended to include a quesiton :) How can I get process.env.MyEnvVar to have multiline instead of escaping it? – Jamil Jan 16 '18 at 15:46
  • Check this : https://stackoverflow.com/questions/30400341/environment-variables-containing-newlines-in-node – Gabriel Bleu Jan 16 '18 at 15:48
  • it looked like a workaround, dotnev seem to support multiline but I can't get it to work https://www.npmjs.com/package/dotenv see "new lines are expanded if in double quotes" – Jamil Jan 16 '18 at 15:50
  • See answer from Allen in above post; One option is to set the variable outside of node with actual newlines – Gabriel Bleu Jan 16 '18 at 15:53

1 Answers1

2

The dotenv library supports new lines. If you use double quotes and the new line character (\n) then the new line character will create a new line.

Example in .env file:

SERVER_KEY = "abc\nefg"

will become:

abc
efg
jakobinn
  • 1,832
  • 1
  • 20
  • 20