twilio cli not reading .env file in node - node.js

I have created a .env file (should it have a name or just the extension?)
In it I have placed my credentials:
TWILIO_ACCOUNT_SID=AC---------------------------
TWILIO_API_KEY=SK------------------------------
TWILIO_API_SECRET=-------------
I have installed the dotenv package from npm and this code works perfectly:
const dotenv = require('dotenv');
const result = dotenv.config()
if (result.error) {
throw result.error
}
console.log(result.parsed)
However, when I try and run the cli command:
twilio phone-numbers:update "+xxxxxxxxxx" --sms-url="http://localhost:1337/sms"
It tells me:
Alternatively, twilio-cli can use credentials stored in environment variables:
# OPTION 1 (recommended)
TWILIO_ACCOUNT_SID = your Account SID from twil.io/console
TWILIO_API_KEY = an API Key created at twil.io/get-api-key
TWILIO_API_SECRET = the secret for the API Key
Trying to use the cli as recommended in the tutorials to update the webhook for my number and run ngrok automatically. Any ideas why the cli wouldn't be seeing the values in the .env when they are clearly accessible in a script via dotenv?

.env file is not standard to create environment variables. In your script you used dotenv package which reads this particular file and sets environment variables for you.
for your twilio cli, you have to set those environment variables explicitly. There are multiple ways to do that e.g.
inline export and then run your command
export TWILIO_ACCOUNT_SID=-- TWILIO_API_KEY=-- TWILIO_API_SECRET=-- twilio phone-numbers:update "+xxxxxxxxxx" --sms-url="http://localhost:1337/sms"
set it in your shell file like .bashrc so that they are always available
use your current .env file like this
export $(cat .env | xargs) && twilio phone-numbers:update "+xxxxxxxxxx" --sms-url="http://localhost:1337/sms"

Related

Type Error: require(..).load is not a function? [duplicate]

I am trying to replicate the import script to get my Firebase RTD data to Algolia. When trying to run the script, it fails and says dotenv.load is not a function.
I have the .env file in the same directory as the index.js file. I have tried moving the .env file around but that doesn't help. Here is the beginning code for the index.js:
const algoliasearch = require('algoliasearch');
const dotenv = require('dotenv');
const firebase = require('firebase');
//load values from the ./env file in this direcotry into process.env
dotenv.load();
//config firebase
firebase.initializeApp({
databaseURL: process.env.FIREBASE_DATABASE_URL,
});
What can I do? Using .config() on the requirement does not help either.
Based on NPM documentation you should use dotenv.config().
This worked for me:
require('dotenv').config({path:'my-app/.env'});
I right-clicked on the .env file to get the relative path.
Maybe it depends on the path of .env file and also, the way you are executing the script. Maybe the firebase commands to run (e.g., firebase serve --only functions) is different of to run node index.js in the path perspective (if you are using a cloud function).
In other words, you need to determine path of your main script and, then, determine the relative path of your .env file to this and set this path to dotenv.config().
To add on to #ahmad's answer, the documentation for the package asks to have you use dotenv.config() to load your .env file. Additionally depending on where you have the path, you'd pass the object { path: /path/to/your/file }. It would possibly help if you required the path package from the nodejs standard library to resolve paths to ensure you're getting the correct path to the file.

I can't access my environment variable in file.js

I define REACT_APP_ADMIN_URL in my .envrc file, i want to use it as a component link, but i get only empty or undefined
This my environment variable, in file .envrc
REACT_APP_ADMIN_URL="http://127.0.0.1:8000/admin"
in file consts.js i make
export const ADMIN_URL = process.env.REACT_APP_ADMIN_URL;
and in my page i make this
import { ADMIN_URL } from './Consts';
<Menu href={ADMIN_URL}>Admin</Menu>
but doesnt work, in my inspector console i get this
Admin
I think that you have to load the environment variables from the .env file on runtime, you can use dotenv package to do so.
Install the package using npm i dotenv
Create a .env file in the root directory of your project. Add environment-specific variables on new lines in the form of NAME=VALUE.
Put this line require('dotenv').config() before using the environment variables on your code.
process.env now has the keys and values you defined in your .env file

How to set environment variables using Node?

I am trying to automatically set three AWS environment variables (AWS_ACCESS_KEY, AWS_ACCESS_ID and AWS_SESSION_TOKEN) which are derived from the JSON returned by aws sts assume-role.
Normally if I wanted to automatically set environment variables I would write a Bash script, say setvars.sh:
export AWS_ACCESS_KEY=something
and then
source setvars.sh
I know that if you do process.env.AWS_ACCESS_KEY = 'something' in a Node script, it won't affect the parent process.
Is there a workaround to be able to use a Node script (rather than Bash, which will be tricky to manipulate JSON with) to set environment variables this way?
When you set environment variables using a bash script and run in a shell, they are accessible just to the processes which are run in the same shell. So you will need to run the Node app in the same shell to access those variables.
Now another approach is to add them in process.env object, so you can write a config script just to load all the config variables and require it at top of your Node app. You need to design the application in such a way that you can use all the configuration in same file.
For Example:
config.js:
process.env.AWS_ACCESS_KEY = 'something'
app.js:
// Starting point of your app
require('./config');
const app = require('express')();
// Use the config
// AWS_API(process.env.AWS_ACCESS_KEY);
// Other App Logic
This approach is mostly used in development environment, in production you might want to use the first approach or you can add the configuration globally using /etc/profile or /etc/environment. Refer how-to-set-global-environment-variable
You can read the JSON file and set your environment variables in process.env:
process.env['environment_variable_name'] = 'environment_variable_value';
NB: This will be available only for that particular node process and its children, not globally available.
As an expansion to the accepted answer,
You can also define your ENV in a file, and import a specific env config based on a release.
This way you can set ENV files for your staging, development or production environments in static configs if required.
From dotenv repository/docs
As early as possible in your application, require and configure dotenv.
require('dotenv').config()
Create a .env file in the root directory of your project. Add
environment-specific variables on new lines in the form of NAME=VALUE.
For example:
DB_HOST=localhost
DB_USER=root
DB_PASS=s1mpl3
process.env now has the keys and values you defined in your .env file.
const db = require('db')
db.connect({
host: process.env.DB_HOST,
username: process.env.DB_USER,
password: process.env.DB_PASS
})
So for you, you may want to do an async call from AWS to get those ENV variables, or perhaps save them within a .env file
https://github.com/motdotla/dotenv

"dotenv.load() is not a function" while trying to run a Node script

I am trying to replicate the import script to get my Firebase RTD data to Algolia. When trying to run the script, it fails and says dotenv.load is not a function.
I have the .env file in the same directory as the index.js file. I have tried moving the .env file around but that doesn't help. Here is the beginning code for the index.js:
const algoliasearch = require('algoliasearch');
const dotenv = require('dotenv');
const firebase = require('firebase');
//load values from the ./env file in this direcotry into process.env
dotenv.load();
//config firebase
firebase.initializeApp({
databaseURL: process.env.FIREBASE_DATABASE_URL,
});
What can I do? Using .config() on the requirement does not help either.
Based on NPM documentation you should use dotenv.config().
This worked for me:
require('dotenv').config({path:'my-app/.env'});
I right-clicked on the .env file to get the relative path.
Maybe it depends on the path of .env file and also, the way you are executing the script. Maybe the firebase commands to run (e.g., firebase serve --only functions) is different of to run node index.js in the path perspective (if you are using a cloud function).
In other words, you need to determine path of your main script and, then, determine the relative path of your .env file to this and set this path to dotenv.config().
To add on to #ahmad's answer, the documentation for the package asks to have you use dotenv.config() to load your .env file. Additionally depending on where you have the path, you'd pass the object { path: /path/to/your/file }. It would possibly help if you required the path package from the nodejs standard library to resolve paths to ensure you're getting the correct path to the file.

How to tell the difference between Heroku Config Variables vs .Env Variables

I'm a native iOS Swift dev and I have lightly dabbled in Node.js so this process is a little foreign to me.
Using node if I want to hide my variables I first import the dotenv module, require it in my app.js file, set .env variables, add it to a .gitignore file, and then use the dotenv module to access them:
// on the cli
$ npm install dotenv --save
$ touch .env
$ touch .gitignore
// in the .env file
MY_SECRET_KEY=12345
// finally add the .env file to the .gitignore filel
// in the app.js file
const dotenv = require('dotenv');
dotenv.load();
initializeSomethingWith(process.env.MY_SECRET_KEY)
In Heroku I set the keys as secret using:
$ heroku config:set MY_SECRET_KEY=123456789
$ git push heroku master
I access the heroku config variable using:
process.env.MY_SECRET_KEY
Where my confusion lies is once I require the dotenv module, how can it tell the difference between whatever is inside the .env file vs whatever I set for the heroku variables?
app.js file:
const dotenv = require('dotenv');
dotenv.load();
var isThisAHerokuKey = process.env.MY_SECRET_KEY // key is 123456789
var orIsThisADotEnvKey = process.env.MY_SECRET_KEY // key is 12345
initializeSomethingWith(process.env.MY_SECRET_KEY) // which key is this using?
Of course common sense would say just don't name the keys the same name.
The recommended way to work with environment variables in node.js on Heroku is NOT to explicitly require('dotenv') in your code.
Instead, set your env vars for your Heroku apps with heroku config:set as you have been doing.
In your development environment, put your local env vars in your .env file, but DON'T add .env to git (i.e. put it in your .gitignore).
Then, use heroku local to run your app (based on your Procfile).
When heroku local executes your app, it takes your env vars defined in .env, so that they can be referenced in your code via process.env.ENV_VAR_XYZ.
See here for more details.
After playing around with this for a while it seems that if I run the file locally by running npm start it will use the value of 12345 but if it’s used remotely on Heroku then it will use the value of 123456789.
const dotenv = require('dotenv');
dotenv.load();
// REMOTE VALUE
var isThisAHerokuKey = process.env.MY_SECRET_KEY // key is 123456789
// LOCAL VALUE
var orIsThisADotEnvKey = process.env.MY_SECRET_KEY // key is 12345
// DEPENDS ON WETHER USED LOCALLY or REMOTELY
initializeSomethingWith(process.env.MY_SECRET_KEY)

Resources