Nodemailer - 弃用警告:“punycode”模块已弃用

Nodemailer - DeprecationWarning: The `punycode` module is deprecated

提问人:ICTDEV 提问时间:11/8/2023 更新时间:11/15/2023 访问量:2520

问:

我需要创建一个脚本,每天使用 gmail 帐户发送一次邮件。我使用了nodemailer,我有这个简单的测试代码

const fs = require('fs');
const path = require('path');
const nodemailer = require('nodemailer');

const transport = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    //secure: true,
    //port: 465,
    auth: {
        user: '[email protected]',
        pass: 'mypassword'
    }
})

let mailOptions = {
    from: '[email protected]', // sender address
    to: '[email protected]', // list of receivers
    subject: 'Test email', // Subject line
    text: 'this is some text', //, // plaintext body
    html: '<b>Hello world </b>' // You can choose to send an HTML body instead
}

transport.sendMail(mailOptions, (error, info) => {
    if( error ) {
        return false;
    } else {
        console.log('Message sent: ' + info.response);
        return true;
    };
});

我注意到,当我尝试使用运行时,我总是会遇到此错误node index.js

(node:20496) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)

代码是否有任何问题,或者是 nodemailer 错误?还有其他选择吗?

javascript 节点.js gmail nodemailer

评论

1赞 Nick Parsons 11/8/2023
看看这个未解决的问题和 andris9(nodemailer 作者)的评论: github.com/nodemailer/nodemailer/issues/1587
0赞 ICTDEV 11/8/2023
那么有什么可以解决这个问题呢?我不知道什么是punycode
0赞 Nick Parsons 11/8/2023
你可以使用不同的 npm 包,或者,你现在可以忽略警告,并希望 Nodemailer 解决这个问题(毕竟,Punycode 自 node v7 以来已被弃用,我们现在在 v21 中)。Punycode 曾经是 node 中的内置模块,但后来已被弃用,可以在 node 的未来版本中删除。Nodemailer 正在使用这个已弃用的版本,这就是给你警告的原因。
0赞 ICTDEV 11/8/2023
我在过去的nodemailer中使用过,但没有发生这个问题。我已经使用了该模块。我希望团队能够解决这个问题。此时,我将选择使用 blat 并使用节点 js 的生成来调用它mail-composer
1赞 Nick Parsons 11/8/2023
我猜该节点将此弃用警告作为节点 v21 的一部分添加,因此以前从未见过。

答:

1赞 David 11/15/2023 #1

我只是遇到了同样的问题。您需要将节点版本降级到 20.9.0。

评论

3赞 ICTDEV 11/15/2023
降级是一个痛苦的过程,最好的解决方案是等到 nodemailer 更新源代码