Reputation: 615
var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');
var nodemailer = require('nodemailer');
var TWILIO_TOKEN = "270ff32fe16828869dc30e0c6926fa9e";
var client = require('twilio')(process.env.AC55a59221acb23a5aa6f046740bb73317, process.env.TWILIO_TOKEN);
router.use(bodyParser.urlencoded({extended: true}));
router.use(bodyParser.json());
router.post('/', function(req, res) {
console.log('this is the req', req.body);
client.messages.creat({
to:'+19522209630',
from:'+17633249718',
body:'hello World'
}, function(err, data) {
if (err) {
console.log('err', err);
console.log('data', data);
}
});//en d of sendMessage
res.send(200);
});
module.exports = router;
/Users/moisesmiguelhernandez/Documents/prime/solo_project/node_modules/twilio/lib/rest/Twilio.js:101
throw new Error('username is required');
^
Error: username is required
at new Twilio (/Users/moisesmiguelhernandez/Documents/prime/solo_project/node_modules/twilio/lib/rest/Twilio.js:101:11)
at initializer (/Users/moisesmiguelhernandez/Documents/prime/solo_project/node_modules/twilio/lib/index.js:8:10)
at Object.<anonymous> (/Users/moisesmiguelhernandez/Documents/prime/solo_project/routes/sendMessage.js:6:31)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Module.require (module.js:513:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/Users/moisesmiguelhernandez/Documents/prime/solo_project/server.js:10:19)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `node server.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/moisesmiguelhernandez/.npm/_logs/2017-07-11T15_02_02_750Z-debug.log
I am getting and error that says username is required. I am trying to use twilio. I followed a youtube video and i have it like he does. Any suggestions on how to fix this? P.S The index file is the terminal error message
Upvotes: 18
Views: 32929
Reputation: 365
Definitely one of the most annoying and time consuming errors, but I finally overcame it, and none of the answers I read here made a difference.
To resolve, I had to set the environmental values programatically. Creating the .env file and adding the values was not good enough.
echo "export TWILIO_ACCOUNT_SID='ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'" > .env
echo "export TWILIO_AUTH_TOKEN='your_auth_token'" >> .env
source .env
Substitute your real values and then copy/paste into the terminal within the same directory you want the .env file to be in. Careful, because after doing this the contents of my existing .env file were deleted and replaced by the two values.
And lastly, the .env file doesn't need to be in the root of your project. In my case, the relevant server operates in the directory where Node.js node_modules are ... In that same directory goes the .env file.
Largely understood from this source.
Upvotes: 0
Reputation: 1
if you use firebase, you can move '.env' file to firebase root folder, than it is okay.
Upvotes: -1
Reputation: 21
In my case, everything was correct except I was using the account_sid and auth_token of test credential instead of Live credential.
Upvotes: 0
Reputation: 276
Save these into a .env file at your root of your folder
TWILIO_TOKEN = "270ff32fe16828869dc30e0c6926fa9e"
TWILIO_ACCOUNT_SID = "AC55a59221acb23a5aa6f046740bb73317"
Then install
npm install dotenv --save
After that you can use these environment variables in your file like this:
require('dotenv');
var client = require('twilio')(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_TOKEN);```
Upvotes: 24
Reputation: 1
The Silly mistake which I made was, I used "SMS_SID: YourSID", It must be "SMS_SID = YourSID".
Upvotes: 0
Reputation: 1
I figured out the issue that I was having with this. When starting up the server, make sure that you are in the directory that contains the .env file.
Upvotes: 0
Reputation: 540
Note -: This is not at all a good way but just a workaround. I will edit the answer as soon I will get the right way. This is just a trick which worked in my case.
const client = require('twilio')(<YOUR_ACCOUNT_SID>, <YOUR_ACCOUNT_KEY>);
basically use the keys directly instead of referencing through any variable.
Upvotes: 2
Reputation: 1250
I was working with a colleague when we encounter the same error. I tried almost all the solutions but didn't work for me (most of the solutions here are the same in a sense).
How we resolve this was amazing: how?
When you are using a file called .env for your environment variables, you need to double checks where you created this file
.env
file must be created in your root directory. That means must be inside your project folder not inside sub-folder that is inside your project.// index.js
// this wrong, notice process.env.AC55a59221acb23a5aa6f046740bb73317 in client variable
TWILIO_TOKEN = "270ff32fe16828869dc30e0c6926fa9e";
var client = require('twilio')(process.env.AC55a59221acb23a5aa6f046740bb73317, process.env.TWILIO_TOKEN);
this is correct
process.env.TWILIO_ACCOUNT_SID
// .env
TWILIO_ACCOUNT_SID=AC55a59221acb23a5aa6f046740bb73317
Interested in the mistake we made, it's the wrong placement of .env
file.
Upvotes: 1
Reputation: 1641
I had this same issue as well. What fixed it for me is doing
then I added my
TWILIO_ACCOUNT_SID=***
TWILIO_AUTH_TOKEN=***
full code:
require('dotenv').config();
const accountSid = process.env.ACCOUNT_SID;
const authToken = process.env.AUTH_TOKEN;
const client = require('twilio')(accountSid, authToken);
client.calls
.create({
url: 'http://demo.twilio.com/docs/voice.xml',
to: process.env.CELL_PHONE,
from: process.env.TWIL_NUM,
})
.then(call => console.log(call.sid))
.catch(err => console.log(err));
Upvotes: 9
Reputation: 2896
I encountered this issue today and was getting message Error: username is required
when running my twilio test. Since I am using dotenv, I have a .env
file with my environment variables, and this is where I made a mistake. We have another app with the SID
and TOKEN
variables, so I copied them and left in the export
keyword, i.e. export TWILIO_ACCOUNT_SID=...
, but if using dotenv
, the export
keyword is not needed. I removed export
and re-run my test script and it all worked, e.g.
TWILIO_ACCOUNT_SID=***
TWILIO_AUTH_TOKEN=***
Upvotes: 1
Reputation: 615
var TWILIO_TOKEN = "270ff32fe16828869dc30e0c6926fa9e";
var TWILIO_ACCOUNT_SID = "AC55a59221acb23a5aa6f046740bb73317";
var client = require('twilio')(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_TOKEN);
@philnash i changed it and fixed the creat typo but the terminal is still saying 'username is required'
Upvotes: 3
Reputation: 73075
Twilio developer evangelist here.
Aside from the typo in creat
that Champa has pointed out in the comments, I think I know where you're going wrong. Your code currently has:
var client = require('twilio')(process.env.AC55a59221acb23a5aa6f046740bb73317, process.env.TWILIO_TOKEN);
I am guessing that it should have something like process.env.TWILIO_ACCOUNT_SID
.
The account sid is effectively the username for accessing the API, which is why the error message says that.
Let me know if that helps at all.
Upvotes: 0