twilio error 'username required'

Viewed 18098

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! solo_project@1.0.0 start: `node server.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the solo_project@1.0.0 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

11 Answers

I had this same issue as well. What fixed it for me is doing

  • npm install dotenv
  • require('dotenv').config()

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));

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.

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=***

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.
  • when you are to use it, make sure you reference the right variable. This mistake is related to this question. such as
// 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

Twilio Documentation

Interested in the mistake we made, it's the wrong placement of .env file.

I have ran into this error earlier so, adding to the answers above make sure the .env file is in the root directory, all spellings are correct and dotenv package is used.

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.

The Silly mistake which I made was, I used "SMS_SID: YourSID", It must be "SMS_SID = YourSID".

In my case, everything was correct except I was using the account_sid and auth_token of test credential instead of Live credential.

Related