newbiedev
newbiedev

Reputation: 3586

dotenv file not readed in node cli script

I've created a .env file inside my node cli script folder. I'm trying to load the variables I've defined inside it by using the process.env.MY_VARIABLE but I've noticed that they are undefined.

at the moment this is the code inside the index.js file of my cli script

#!/usr/bin/env node

const fb = require('facebook-chat-api'); 

//debug only 
console.log(process.env.FB_EMAIL, process.env.FB_PWD)

let credentials = { email: process.env.FB_EMAIL, password: process.env.FB_PWD }

fb(credentials, (err, api) => {
     if(err) return console.log(err);
     console.log(api);
});

and this is the content of my .env file that is in the same directory of package.json file and index.js

FB_EMAIL="[email protected]"
FB_PWD="apasswprd"

Do I miss something?

Upvotes: 0

Views: 1807

Answers (1)

Georgios Kampitakis
Georgios Kampitakis

Reputation: 475

How are you loading the .env file ? This mapping of the .env to the process.env doesn't happen automatically. You can have a look on https://www.npmjs.com/package/dotenv for loading an .env file to you process.env.

Another way of doing that is having FB_EMAIL and FB_PWD in your environment from where you run that cli tool. You could try on your terminal export FB_EMAIL=****

Upvotes: 1

Related Questions