So, i am coming from a LAMP stack and all this new js is quite new to me.
I have written an email sending module for my little app which is executed as follows:
var emailer = require( '../../app/emailer.js');
emailer({
tplName : 'activationEmail',
tplVariables : {name:'john',activationLink:'bob'},
sendToEmail: 'john@gmail.com',
sendToName: 'John',
fromEmail : 'noReply',
subject: 'Activation email'
});
Now this works just fine however i am not sure what is better (as i am missing some fundamental nodejs knowledge), to run as above or run as this:
require( '../../app/emailer.js')({
tplName : 'activationEmail',
tplVariables : {name:'john',activationLink:'bob'},
sendToEmail: 'john@gmail.com',
sendToName: 'John',
fromEmail : 'noReply',
subject: 'Activation email'
});
As you can see here this is triggereing an activation email, the emailer module is being called from within a passport module.
My question, should I 'require' the module into a var then reference the var later, or require the emailer module each time i need it.. i'm not sure what the difference is..
A NooB Q i know, but i can't find a solid answer on google.
Thanks in advance, John
No, there is no benefit to storing it in a variable unless you will use that module more than once.
If I am writing a very short nodejs project, and need to use a single module just one time, I often don't bother to store it in a variable, I'd just do what you have done.
However, if I know I will be reusing that module, I store it, so that when reusing my program isn't slowed by the require lookup every time.
Also, if I am using other modules as well, I like to declare everything at the top of my file, even one-time modules.
var fs = require('fs'), // will use more than once
http = require('http'), // will use more than once
qr = require('qr-codes'), // will use more than once
ip = require('ip'); // will only use once
I just think it looks neater.