title: Nodejs - Global or local initialisation of libraries
body: <p>I am a bit confused as to which of the below is the better practice and why? In the first, we initialise the library globally and in the second case, its done inside a function.</p>
  
  <p>Code 1 </p>
  
  <pre><code>var twilio_client = require('twilio')(
    process.env.TWILIO_ACCOUNT_SID,
    process.env.TWILIO_AUTH_TOKEN
  );
  
  function_sendSMS(from, to){
     twilio_client.messages.create({
       from: from,
       to: to,
       body: "You just sent an SMS from Node.js using Twilio!"
       }, function(err, message) {
       if(err) {
        console.error(err.message);
       }
    });
  }
  </code></pre>
  
  <p>Code 2</p>
  
  <pre><code>function_sendSMS(from, to){
  
     var twilio_client = require('twilio')(
       process.env.TWILIO_ACCOUNT_SID,
       process.env.TWILIO_AUTH_TOKEN
     );
  
     twilio_client.messages.create({
       from: from,
       to: to,
       body: "You just sent an SMS from Node.js using Twilio!"
       }, function(err, message) {
       if(err) {
        console.error(err.message);
       }
    });
  }
  </code></pre>