0

I've written a node.js purger and I need to bring in environmental variables.

How to do this correctly? I've tried a few methods from how I remember doing this in Rails, ways in the past, and trying to dig it out online but I haven't gotten it to work yet.

Thanks!

My latest try:

  var clientToken = process.env.AKAMAI_CLIENT_TOKEN;
  var clientSecret = process.env.AKAMAI_ACCESS_TOKEN;
  var accessToken = process.env.AKAMAI_SECRET;
  var host = process.env.AKAMAI_HOST;

  var config = {
    clientToken: clientToken,
    clientSecret: clientSecret,
    accessToken: accessToken,
    host: host,
    queue: "default"
  };

many thanks!

add-on: error:

readline.js:925
            throw err;
            ^

TypeError: Cannot read property 'replace' of null
    at Authenticator.makeDataToSign (/Users/homeImac/purger/node_modules/akamai-ccu-purge/src/auth.js:88:23)
at Authenticator.generateSignature (/Users/homeImac/purger/node_modules/akamai-ccu-purge/src/auth.js:100:25)
at Authenticator.generateAuthHeaderForRequest (/Users/homeImac/purger/node_modules/akamai-ccu-purge/src/auth.js:122:24)
at Purger.purgeObjects (/Users/homeImac/purger/node_modules/akamai-ccu-purge/src/purger.js:30:39)
at /Users/homeImac/purger/node_modules/akamai_purge/newPurge.js:28:10
at /Users/homeImac/purger/node_modules/akamai_purge/getUrl2.js:9:14
at /Users/homeImac/purger/node_modules/prompt/lib/prompt.js:316:32
at /Users/homeImac/purger/node_modules/utile/node_modules/async/lib/async.js:142:25
at assembler (/Users/homeImac/purger/node_modules/prompt/lib/prompt.js:313:9)
at /Users/homeImac/purger/node_modules/prompt/lib/prompt.js:322:32

I'll add my bash code just to cover all bases as well. It's still not working for me.

export AKAMAI_CLIENT_TOKEN="dfghdfhdfhgfhdfghdfghfghfdghfg"
export AKAMAI_ACCESS_TOKEN="fhdfghdfghdfhdfghfhfghdfg"
export AKAMAI_SECRET="dfhfdghdfhgfdhgfhdhgfhdgfh="
export AKAMAI_HOST="fghghdfhdhhgfdhgfhdghdfgf.purge.akamaiapis.net"

That's all i need in the bash file right? There's an = at the end of the 3rd line. That shouldn't cause any issues right? Thanks for the help folks!

Additional note: I added quotes around the values thanks to a tip I received below. I can still console.log the info just fine but I still receive this error.

nyhunter77
  • 614
  • 2
  • 7
  • 19
  • 2
    `process.env.XXX` should work. Were you not getting the values you expected? Have you tried printing your environment variables *before* running Node then verifying that Node is getting the same values? – Mike Cluck Apr 13 '16 at 21:59
  • The second piece of code is correct. In what sense does it not work? Have you added `console.log()` calls to see if the environment variables contain what you expect? – Pointy Apr 13 '16 at 21:59
  • So I did console log it out and it does get read in and will console.log out, thanks. Can you spot if I did anything incorrectly in the config object? I'm guessing that's where the issue is, but I don't see anything that's incorrect. Thanks! – nyhunter77 Apr 13 '16 at 22:25

1 Answers1

0

Read environment variables in Node.js

Your second answer is right. If the environment variables aren't showing up they may not be available to the node process on startup. Try doing echo $AKAMAI_HOST to see if its available in your bash where you start node

Community
  • 1
  • 1
jpopesculian
  • 712
  • 3
  • 11
  • Yep, this did work, I wonder if I did something wrong trying to read it into the config variable, can I take process.env.AKAMAI_CLIENT_TOKEN and place that inside the config variable, I'd try it now but I'm traveling at the moment. thanks for the info! I tried at first to do this but I was probably using the wrong syntax. – nyhunter77 Apr 13 '16 at 22:27
  • Of course, glad it worked! You can absolutely take `process.env.AKAMAI_CLIENT_TOKEN` and put it directly into the config object – jpopesculian Apr 13 '16 at 22:33
  • Actually, now that I've sat down with the code again, i guess it's something to do with the API itself for Akamai, could it be that there is some security feature that doesn't allow "replacing" the info with variable information? I'll post the error above – nyhunter77 Apr 14 '16 at 00:32
  • I'm not near my computer now either but try an `echo $AKAMAI_SECRET`. The extra equals at the end may cause an issue. You can wrap the environment variable values in quotation marks – jpopesculian Apr 14 '16 at 01:22
  • Sadly, this didn't solve the issue either, same error. Thanks though! – nyhunter77 Apr 14 '16 at 01:51