Manuel Rueda Blog

Environment variable string encoding

October 10, 2015 - 1 minute read


DATE: February 2019

This is a blog entry that I wrote before I change my blog, most of it may be old and probably outdated.

When i start working on this blog (based in ghost), i put a bunch of sensitive data in environment variables in the heroku configuration.

The problem was a key of google drive API, the content is a multi-line string and can’t make it work in one line. When i put the new line character into the string \n, it wasn’t interpreted by node.

The only solution that i found for this problem was this, convert the string to a buffer and then to string again.

// set the TEST environment variable with TEST=first line\\n\\nsecond line\\n
// you need to duplicate the \ to scape it
function parseEnvVarible(envVar){
return new Buffer(envVar.split('\\n').join(require('os').EOL), 'UTF-8').toString('UTF-8');
console.log('Wrong: ' + process.env.TEST);
console.log('Right: ' + parseEnvVarible(process.env.TEST));
view raw example.js hosted with ❤ by GitHub

My name is Manuel Rueda and welcome to my blog. You can also follow me on Twitter and/or Github.