'npm Twitter library not authenticating?

I have this code

var Twitter = require('twitter');

var client = new Twitter({

        consumer_key: process.env.TWITTER_CONSUMER_KEY,
        consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
        access_token_key: process.env.TWITTER_ACCESS_KEY,
        access_token_secret: process.env.TWITTER_ACCESS_SECRET,
});


client.post('status/update', {status: 'Test'}, function(error,params,response){
    if(error) throw;
    console.log(params);
    console.log(response);
});

but it keeps just giving me this when i run node tweet.js

if(error) throw;
               ^
SyntaxError: Unexpected token ;
  at Module._compile (module.js:439:25)
  at Object.Module._extensions..js (module.js:474:10)
  at Module.load (module.js:356:32)
  at Function.Module._load (module.js:312:12)
  at Function.Module.runMain (module.js:497:10)
  at startup (node.js:119:16)
  at node.js:929:3

I am 100% sure that my environment variables are correct and exactly what they say on my dev twitter. So i am not sure what the problem is here or even how to approach it!

After fixing the syntax error and changing that line to throw error; the new problem is it is now this

    if(error) throw error;
                    ^
    [object Object]


Solution 1:[1]

Try following code for client.post

client.post('status/update', {status: 'Test'}, function(error,params,response){
    if(error) throw error;
    console.log(params);
    console.log(response);
});

Actually you forgot to throw the error. So there was a syntax error.

Solution 2:[2]

error in your case is not a standard javascript Error object and hence it cannot be thrown this way, you may check the value of error object and see what it contains and through if statement you can handle your actions, alternatively you may throw a custome error by issuing throw 'OMG!';

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kelsadita
Solution 2 Ma'moon Al-Akash