Reputation: 3792
I have two files, my app.js and my mailer module called mailer.js.
My app.js should send several emails when starting the application.
const express = require('express');
const app = express();
const mailer = require('./Server/mailer');
mailer.sendHtmlMail(["email1", "email2"], "Test Email", "<p>Success!</p>");
app.listen(8888, function () {
console.log('Server running on port 8888');
});
And my mailer.js executes the mail service
const nodemailer = require('nodemailer');
const senderMail = "myEmail";
const emailTransporter = nodemailer.createTransport({
service: 'yahoo',
auth: {
user: senderMail,
pass: 'pw'
}
});
function getMailReceivers(mailReceivers){ // convert the string array to one string
var receivers = "";
for(var i = 0; i < mailReceivers.length; i++){
receivers += mailReceivers[i];
if(i < mailReceivers.length - 1)
receivers += ", ";
}
return receivers;
}
function getMailOptions(mailReceivers, subject, html){ // set the mail options and return them
return {
from: senderMail,
to: getMailReceivers(mailReceivers),
subject: subj,
html: content
};
}
module.exports = function () { // export the sendMail function here
sendHtmlMail: function(mailReceivers, subject, html){ // send the email
emailTransporter.sendMail(getMailOptions(mailReceivers, subject, html), function(error, info){
if (error) {
throw error;
} else {
console.log(info.response);
}
});
}
};
I get this error message
SyntaxError: Unexpected token (
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:599:28)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Module.require (module.js:579:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (C:\...\app.js:3:16)
but I don't understand it, does
at Object. (C:...\app.js:3:16)
say there is an error in my mailer object (line 3) on line 16? I can't find any syntax error there..
Upvotes: 0
Views: 935
Reputation: 1364
You should export an object like that (not a function):
module.exports = {
sendHtmlMail: function(mailReceivers, subject, html){ // send the email
emailTransporter.sendMail(getMailOptions(mailReceivers, subject, html), function(error, info){
if (error) {
throw error;
} else {
console.log(info.response);
}
});
}
}
Now, in your app.js you can import sendHtmlMail
const { sendHtmlMail } = require('./mailer');
And use it like this:
sendHtmlMail(["email1", "email2"], "Test Email", "<p>Success!</p>");
Or:
const mailer = require('./mailer');
mailer.sendHtmlMail(["email1", "email2"], "Test Email", "<p>Success!</p>");
I hope you will find this information useful
Upvotes: 1
Reputation: 48250
I would start from correcting
sendHtmlMail: function(mailReceivers, subject, html){
to
function sendHtmlMail(mailReceivers, subject, html){
or, if you really need the object literal syntax, find a valid place in your code where this is allowed - it definitely is an error in the scope it is defined.
Edit: it is possible that you wanted to export a function returning an object, that would be
module.exports = function () { // export the sendMail function here
return {
sendHtmlMail: function (mailReceivers, subject, html) { // send the email
emailTransporter.sendMail(getMailOptions(mailReceivers, subject, html), function (error, info) {
if (error) {
throw error;
} else {
console.log(info.response);
}
});
}
}
or simply an object with the function
module.exports = { // export the sendMail function here
sendHtmlMail: function (mailReceivers, subject, html) { // send the email
emailTransporter.sendMail(getMailOptions(mailReceivers, subject, html), function (error, info) {
if (error) {
throw error;
} else {
console.log(info.response);
}
});
}
}
Upvotes: 1
Reputation: 413757
The end of your mailer.js file is where the problem is. That exception you see comes from line 3 of app.js because that's where you require()
the other file.
The problem is that you've mixed up function instantiation with object initiailzation:
module.exports = function () { // export the sendMail function here
sendHtmlMail: function(mailReceivers, subject, html){ // send the email
emailTransporter.sendMail(getMailOptions(mailReceivers, subject, html), function(error, info){
if (error) {
throw error;
} else {
console.log(info.response);
}
});
}
};
Your code expects that module to export an object with a sendHtmlMail
property, so that should be an object initializer:
module.exports = { // export the sendMail function here
sendHtmlMail: function(mailReceivers, subject, html){ // send the email
emailTransporter.sendMail(getMailOptions(mailReceivers, subject, html), function(error, info){
if (error) {
throw error;
} else {
console.log(info.response);
}
});
}
};
Upvotes: 1