nodemailer 在 Nextjs 应用程序中出现错误

nodemailer is giving errors in Nextjs app

我在观看 youtube tutorial 时使用 nextjs 和 nodemailer 构建了一个简单的邮件应用程序并且我做了教程中的所有内容,但是我收到错误我不知道如何解决。下面是我的代码,请帮助并建议我应该如何解决这些错误

import React, { useState } from "react";
import classes from './email.module.css'

const Email = () => {

    const [text, setText] = useState('')
    const handleClick = () => {
        const nodemailer = require('nodemailer')

        const transport = nodemailer.createTransport({
            service: 'hidden',
            port: 465,
            secure: true,
            auth: {
                user: 'hidden',
                pass: '********',
            },                      
        })

        const options = {
            from: 'hidden',
            to: 'hidden',
            subject: 'my test email',
            text: text
        }

        transport.sendMail(options, (err, info) => {
            if(err) {
                console.log('Error: ',err)
            }
            console.log('message sent: ', info.response)
        })
    }

  return (
    <div className={classes.cont}>
      <textarea value={text} onChange={(e) => setText(e.target.value)} className={classes.text}/>
      <button onClick={handleClick} className={classes.but}>send Email</button>
    </div>
  );
};

export default Email;

以下是我的客户端控制台错误

next-dev.js?3515:32 ./node_modules/nodemailer/lib/dkim/index.js:10:0
Module not found: Can't resolve 'fs'

Import trace for requested module:
./node_modules/nodemailer/lib/mailer/index.js
./node_modules/nodemailer/lib/nodemailer.js
./comp/Email.js
./pages/index.js

https://nextjs.org/docs/messages/module-not-found
window.console.error @ next-dev.js?3515:32
handleErrors @ hot-dev-client.js?1600:117
processMessage @ hot-dev-client.js?1600:167
eval @ hot-dev-client.js?1600:29
eval @ websocket.js?a9be:58
handleMessage @ websocket.js?a9be:57
next-dev.js?3515:32 ./node_modules/nodemailer/lib/mailer/index.js:14:0
Module not found: Can't resolve 'dns'

Import trace for requested module:
./node_modules/nodemailer/lib/nodemailer.js
./comp/Email.js
./pages/index.js

https://nextjs.org/docs/messages/module-not-found
window.console.error @ next-dev.js?3515:32
handleErrors @ hot-dev-client.js?1600:117
processMessage @ hot-dev-client.js?1600:167
eval @ hot-dev-client.js?1600:29
eval @ websocket.js?a9be:58
handleMessage @ websocket.js?a9be:57
next-dev.js?3515:32 ./node_modules/nodemailer/lib/mime-node/index.js:7:0
Module not found: Can't resolve 'fs'

Import trace for requested module:
./node_modules/nodemailer/lib/mail-composer/index.js
./node_modules/nodemailer/lib/mailer/index.js
./node_modules/nodemailer/lib/nodemailer.js
./comp/Email.js
./pages/index.js

https://nextjs.org/docs/messages/module-not-found
window.console.error @ next-dev.js?3515:32
handleErrors @ hot-dev-client.js?1600:117
processMessage @ hot-dev-client.js?1600:167
eval @ hot-dev-client.js?1600:29
eval @ websocket.js?a9be:58
handleMessage @ websocket.js?a9be:57
next-dev.js?3515:32 ./node_modules/nodemailer/lib/sendmail-transport/index.js:3:0
Module not found: Can't resolve 'child_process'

Import trace for requested module:
./node_modules/nodemailer/lib/nodemailer.js
./comp/Email.js
./pages/index.js

https://nextjs.org/docs/messages/module-not-found
window.console.error @ next-dev.js?3515:32
handleErrors @ hot-dev-client.js?1600:117
processMessage @ hot-dev-client.js?1600:167
eval @ hot-dev-client.js?1600:29
eval @ websocket.js?a9be:58
handleMessage @ websocket.js?a9be:57
next-dev.js?3515:32 ./node_modules/nodemailer/lib/shared/index.js:7:0
Module not found: Can't resolve 'fs'

Import trace for requested module:
./node_modules/nodemailer/lib/nodemailer.js
./comp/Email.js
./pages/index.js

https://nextjs.org/docs/messages/module-not-found
window.console.error @ next-dev.js?3515:32
handleErrors @ hot-dev-client.js?1600:117
processMessage @ hot-dev-client.js?1600:167
eval @ hot-dev-client.js?1600:29
eval @ websocket.js?a9be:58
handleMessage @ websocket.js?a9be:57
next-dev.js?3515:32 ./node_modules/nodemailer/lib/shared/index.js:9:0
Module not found: Can't resolve 'dns'

Import trace for requested module:
./node_modules/nodemailer/lib/nodemailer.js
./comp/Email.js
./pages/index.js

https://nextjs.org/docs/messages/module-not-found
window.console.error @ next-dev.js?3515:32
handleErrors @ hot-dev-client.js?1600:117
processMessage @ hot-dev-client.js?1600:167
eval @ hot-dev-client.js?1600:29
eval @ websocket.js?a9be:58
handleMessage @ websocket.js?a9be:57
next-dev.js?3515:32 ./node_modules/nodemailer/lib/smtp-connection/http-proxy-client.js:8:0
Module not found: Can't resolve 'tls'

Import trace for requested module:
./node_modules/nodemailer/lib/mailer/index.js
./node_modules/nodemailer/lib/nodemailer.js
./comp/Email.js
./pages/index.js

https://nextjs.org/docs/messages/module-not-found
window.console.error @ next-dev.js?3515:32
handleErrors @ hot-dev-client.js?1600:117
processMessage @ hot-dev-client.js?1600:167
eval @ hot-dev-client.js?1600:29
eval @ websocket.js?a9be:58
handleMessage @ websocket.js?a9be:57
next-dev.js?3515:32 ./node_modules/nodemailer/lib/smtp-connection/index.js:6:0
Module not found: Can't resolve 'tls'

Import trace for requested module:
./node_modules/nodemailer/lib/smtp-pool/index.js
./node_modules/nodemailer/lib/nodemailer.js
./comp/Email.js
./pages/index.js

https://nextjs.org/docs/messages/module-not-found

nodemailer 是一个 Node.js 库,不能在客户端使用。这就是您看到错误 Can't resolve 'fs'.

的原因

您可以改为创建 an API route(将在服务器端执行)并让客户端应用程序调用 API 来执行邮件任务。