使用 Nodemailer 和 Webpack 时无法解析 dns 和 child_process

2023-11-26

我正在尝试使用 nodemailer 发送一封简单的电子邮件,但遇到以下问题。看来问题出在 webpack 上。我已经包含了错误和有问题的代码。谢谢!

控制台日志:

ERROR in ./~/nodemailer/lib/mailer/index.js
Module not found: Error: Can't resolve 'dns' in 
'/Users//Users/user/Projects/world-domination/project-folder/node_modules/nodemailer/lib/mailer'
@ ./~/nodemailer/lib/mailer/index.js 14:12-26
@ ./~/nodemailer/lib/nodemailer.js
@ ./src/app/components/contact/contact.component.ts
@ ./src/app/app.module.ts
@ ./src/main.ts
@ multi webpack-dev-server/client?http://localhost:4200 ./src/main.ts

ERROR in ./~/nodemailer/lib/sendmail-transport/index.js
Module not found: Error: Can't resolve 'child_process' in 
'/Users/user/Projects/world-domination/project-folder/node_modules/nodemailer/lib/sendmail-transport'
@ ./~/nodemailer/lib/sendmail-transport/index.js 3:14-38
@ ./~/nodemailer/lib/nodemailer.js
@ ./src/app/components/contact/contact.component.ts
@ ./src/app/app.module.ts
@ ./src/main.ts
@ multi webpack-dev-server/client?http://localhost:4200 ./src/main.ts

有问题的代码:

import { Component, OnInit } from '@angular/core';
import * as nodemailer from 'nodemailer';

@Component({
  selector: 'app-contact',
  templateUrl: './contact.component.html',
  styleUrls: ['./contact.component.scss']
})
export class ContactComponent implements OnInit {
    public sendMessage(): void {
        let transporter = nodemailer.createTransport({
          host: 'smtp.example.com',
          port: 465,
          secure: true,
          auth: {
            user: '[email protected]',
            pass: 'samplePassword'
         }
    });

    let mailOptions = {
        from: '"Mr. Sender" <[email protected]>',
        to: '[email protected]',
        subject: 'Test email subject',
        text: 'Test email body'
    };

    transporter.sendMail(mailOptions, (error, info) => {
      if (error) {
        return console.log(error);
      }
      console.log('Message %s sent: %s', info.messageId, info.response);
    })
  }
}

你根本不能在前端使用nodemailer。 Nodemailer 和其他依赖它的项目(即 gmail-send)是为在后端使用 Nodejs 而制作的。

相反,您应该考虑使用第 3 方 smtp 服务(例如 AWS-SES),或者通过在后端使用 nodemailer 来创建自己的 smtp 服务,前端将通过 https 请求之类的方式调用它。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Nodemailer 和 Webpack 时无法解析 dns 和 child_process 的相关文章

  • Angular 2\4 哈希 url 保留index.html

    背景 Angular 4 ng cli RouterModule useHash true 当我使用浏览到我的应用程序时http server index html它决定http server url 中省略了index html 此外 每
  • 如何从 Angular 7 中的另一个组件更新视图?

    我想从导航栏刷新我的卡集 该导航栏是应用程序组件 html所以我准备了刷新 功能 当它被调用时 它会更新变量 Cards 但不会将其呈现在ngFor在 html 元素上mainView html 如果我从 html 元素调用 它会渲染更新的
  • 角度2中未定义的值

    我正在创建一个应用程序 在其中使用 Ag Grid api 在网页上列出我的数据库内容 Ag grid 有一个预定义的 api 来获取所选行的内容 这是我的代码 export class customer entryComponent pu
  • 语法错误:elasticdump 中的exports.runInThisContext (vm.js:53:16) 处出现意外标记 {

    我试图使用elasticdump 将elasticsearch 数据从一台服务器移动到另一台服务器 当我给予 elasticdump input http 192 168 0 122 9200 my index output http lo
  • 添加 PayPal 付款方式 Ionic

    我正在尝试在我的用 Ionic 2 3 制作的应用程序上实现 PayPal 付款方式 我已经安装了 ionic cordova plugin add com paypal cordova mobilesdk npm install save
  • nodejs mocha suite 未定义错误

    我正在尝试使用摩卡运行一些测试 但似乎无法克服这个错误 E tdd nodejs cart gt mocha cart test js node js 201 throw e process nextTick error or err Re
  • express 或express-generator:我需要两者吗?

    只是探索 Node js 并遇到了 Express 在 npm 存储库站点上https www npmjs com package express https www npmjs com package express它明确指出安装是 np
  • npm install 命令下载所需包的源位置是什么?

    我试图获取命令 npm install 尝试连接的源位置 URL 并根据 package json 文件获取要下载的依赖包 并将其放置在本地框中 从下面提到的网址 http www tutorialspoint com nodejs nod
  • aspnetcore 出现角度错误 NodeInitationException:节点调用在 60000 毫秒后超时

    我在用着yo generator aspnetcore spa一旦我运行应用程序 我就遇到了问题 处理请求时发生未处理的异常 NodeIncationException 节点调用在 60000 毫秒后超时 您可以通过设置更改超时持续时间 N
  • Angular4 屏蔽输入中的字符而不更改其值

    如何屏蔽输入中除 Angular 4 中最后四个字符之外的所有字符 即显示 而不更改实际值 例如 输入文本框中的数字应类似于 1234 且值应为 7671234 使用指令 Directive selector stringHide expo
  • 使用 Node.js 构建网站的最佳实践

    这个问题的答案是社区努力 help privileges edit community wiki 编辑现有答案以改进这篇文章 目前不接受新的答案或互动 我想知道如何使用 Node js 从头开始 开发一个网站 我明白我怎么能possibly
  • 护照:登录和帐户注册的不同重定向

    我在我的应用程序中使用护照模块 github身份验证 我想根据操作进行重定向 我检查这是否只是正常登录或者用户是否第一次登录 passport use new GitHubStrategy clientID conf github app
  • 如何在父视图和子视图中使用组件两次(DI 是共享的,comp 是单例)

    我有一个按钮组件 具有应用程序特定行为 我打算在应用程序中广泛使用它 问题是 当我有一个使用此按钮的父 子视图时 单击父按钮会触发子视图按钮的 操作 如果不这样做 您自然不会明白发生了什么有多年的面向对象编程经验 让年轻的学校新手使用 da
  • 使用 AWS CDK 为 lambda 指定自定义角色

    我意识到它很新 但我没有看到任何语言的任何示例 说明如何为使用 AWS CDK 创建的 lambda 指定角色 我正在尝试这样做 const cdk require aws cdk cdk const lambda require aws
  • 模板解析:找不到管道

    我收到错误 模板解析错误 找不到管道 amDateFormat 这是我的app module ts import NgModule from angular core import MomentModule from angular2 mo
  • 带有 npm 启动脚本的 Nodejs 应用程序

    我对nodejs很陌生 在我的docker化环境中 我想为nodejs应用程序提供appdynamics支持 这要求每个应用程序都要求将以下内容作为其应用程序的第一行 require appdynamics profile controll
  • 订阅内循环订阅?

    我目前正在与订阅内的 multiples forEach 订阅作斗争 我正在尝试检索对象列表 然后通过它们的 ID 检索它们的图像 目前我已经这样做了 this appTypeService get pipe map apps AppTyp
  • Angular 模板验证表单

    我正在研究表单验证 我正在使用模板驱动的验证表单 其编码如下 现在工作正常 但是当我尝试添加时 username ngModel and password ngModel 在创建验证类的输入中 它向我显示一个错误 也请找到该错误 div c
  • Webpack 4:如何使用 LESS 获取 CSS 源映射?

    多年来我一直在尝试让 CSS 源映射在 webpack 中工作 但没有成功 我不确定链条中哪里出了问题 我希望有人能指出我正确的方向 这是发生的事情 行号是错误的 实际上文件名也是错误的 main less只是包含一堆 import也就是说
  • 我想在“npm install”上下载一些资源,大概使用“预发布”脚本

    具体来说 我想在运行 npm install 时下载 angular min js 从我读到的here https npmjs org doc scripts html做这些事情的地方是在预发布脚本 它还提到我不需要依赖在系统上安装 wge

随机推荐