Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node multiline process.env (with and without) dotenv

Tags:

node.js

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

like image 537
Jamil Avatar asked Mar 15 '26 17:03

Jamil


1 Answers

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
like image 107
jakobinn Avatar answered Mar 17 '26 13:03

jakobinn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!