问题
I have a very simple application, just starting to get my hands dirty with nodemailer. When I run the app I get errors in the module itself.
app.js:
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport('smtps://me%40gmail.com:supersecretpw@smtp.gmail.com');
var mailOptions = {
from: '"Me" <me@gmail.com>', // sender address
to: 'him@him.com, her@her.com', // list of receivers
subject: 'Hello dude', // Subject line
text: 'Test email with text', // plaintext body
html: "Testing 1..2..7" // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
And I get this error:
C:\Users\Andrew\desktop\messy4\node_modules\nodemailer\lib\mailer\index.js:31
compile: [(...args) => this._convertDataImages(...args)],
^^^
SyntaxError: Unexpected token ...
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:387:25)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at Object.<anonymous> (C:\Users\Andrew\desktop\messy4\node_modules\nodemailer\lib\nodemailer.js:3:16)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
Really not sure what's happenning here.
回答1:
Node.js version 6+ is required. Check your Node version with the following command:
node --version
If you are not at least 6+ then you must upgrade.
You may receive another error message instructing you to login to your account. In that case, go to your email inbox and you will see a message from Google with a link to a page for setting up less secure app permissions.
回答2:
Simply Add this in your package.json file.
"engines": {
"node": "6.9.4" // You can use any version
}
This will automatically download the node version you provide. By this way you don't need to upgrade your NodeJS every time in server.
回答3:
Nodemailer is compatible with Node version 6 or above (as per https://nodemailer.com/about/#requirements)
So follow these steps to upgrade node:
1* sudo npm cache clean -f
2* sudo npm install -g n
3* sudo n stable
4* sudo ln -sf /usr/local/n/versions/node/5.4.1/bin/node /usr/bin/node (the bold text/version should be the one installed in during above step.)
ie if 8.1.1 is installed then do sudo ln -sf /usr/local/n/versions/node/8.1.1/bin/node /usr/bin/node
node –v (Should show updated version now)
Answered here as well Nodemailer error when required
来源:https://stackoverflow.com/questions/42045637/nodemailer-error-cant-fix