cphill cphill - 1 year ago 128
Javascript Question

Nodejs Module Exports TypeError

I am trying to determine what is wrong with my module setup that is throwing the following error:

[TypeError: transactionalEmails.request is not a function]`

It doesn't appear to be how the variable is being exported, but how I am creating the
function. Any help on what I'm missing or the mistake that I have made?


var transactionalEmails = function() {

var helper = require('sendgrid').mail;

request: function request(requester, receiver){

var fromEmail = new helper.Email(requester);
var toEmail = new helper.Email(receiver);

var subject = requester + ' has requested a message.';
var body = new helper.Content('text/plain', 'Hello Email');
var email = new helper.Mail(fromEmail, subject, toEmail, body);

var sg = require('sendgrid')(process.env.EMAIL_API_KEY);

var request = sg.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: email.toJSON(),

return console.log(fromEmail + " Sending a request to " + toEmail);

/*sg.API(request, function(error, response) {
module.exports = transactionalEmails;

Here is how I am calling the function:

var transactionalEmails = require('./transactional-emails');

transactionalEmails.request(req.user.email, req.body.receiverEmail);

Answer Source

You are exporting a function where you want to export an object.

var helper = require('sendgrid').mail;

var transactionalEmails = {
  request: function request(requester, receiver){

module.exports = transactionalEmails;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download