Angular : Nodemailer 显示大量运行时错误

Angular : Nodemailer shows a lot of runtime error

在我的angular项目中,我想使用Nodemailer来发送邮件。

第一个问题是当我尝试导入时(我的意思是在执行 npm i --save 之后)我执行 ionic serve 时发生了很多错误。我想重复一件事:就在写完 import 行之后,这个大错误日志出现并且应用程序像那样崩溃

错误最多的是这个缺失,那个没有找到但是为什么?当我执行 npm i --save 时,没有任何错误。此外,我的代码中没有语法错误。我唯一的目标是 运行 sendMail() 函数。

我只是不明白是什么原因造成的?我应该将 nodemail 更改为其他内容吗?请提出建议。

错误日志:

Microsoft Windows [Version 10.0.19041.264]
(c) 2020 Microsoft Corporation. All rights reserved.

C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master>ionic serve
> ng.cmd run app:serve --host=localhost --port=8100
[ng] chunk {} 0.js, 0.js.map () 31.1 kB  [rendered]
[ng] ERROR in ./node_modules/nodemailer/lib/sendmail-transport/index.js
[ng] Module not found: Error: Can't resolve 'child_process' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\sendmail-transport'
[ng] ERROR in ./node_modules/nodemailer/lib/dkim/index.js
[ng] Module not found: Error: Can't resolve 'crypto' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\dkim'
[ng] ERROR in ./node_modules/nodemailer/lib/dkim/sign.js
[ng] Module not found: Error: Can't resolve 'crypto' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\dkim'
[ng] ERROR in ./node_modules/nodemailer/lib/dkim/relaxed-body.js
[ng] Module not found: Error: Can't resolve 'crypto' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\dkim'
[ng] ERROR in ./node_modules/nodemailer/lib/mailer/index.js
[ng] Module not found: Error: Can't resolve 'crypto' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\mailer'
[ng] ERROR in ./node_modules/nodemailer/lib/mime-node/index.js
[ng] Module not found: Error: Can't resolve 'crypto' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\mime-node'
[ng] ERROR in ./node_modules/nodemailer/lib/smtp-connection/index.js
[ng] Module not found: Error: Can't resolve 'crypto' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\smtp-connection'
[ng] ERROR in ./node_modules/nodemailer/lib/xoauth2/index.js
[ng] Module not found: Error: Can't resolve 'crypto' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\xoauth2'
[ng] ERROR in ./node_modules/nodemailer/lib/mailer/index.js
[ng] Module not found: Error: Can't resolve 'dns' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\mailer'
[ng] ERROR in ./node_modules/nodemailer/lib/shared/index.js
[ng] Module not found: Error: Can't resolve 'dns' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\shared'
[ng] ERROR in ./node_modules/nodemailer/lib/dkim/index.js
[ng] Module not found: Error: Can't resolve 'fs' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\dkim'
[ng] ERROR in ./node_modules/nodemailer/lib/mime-node/index.js
[ng] Module not found: Error: Can't resolve 'fs' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\mime-node'
[ng] ERROR in ./node_modules/nodemailer/lib/shared/index.js
[ng] Module not found: Error: Can't resolve 'fs' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\shared'
[ng] ERROR in ./node_modules/nodemailer/lib/fetch/index.js
[ng] Module not found: Error: Can't resolve 'http' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\fetch'
[ng] ERROR in ./node_modules/nodemailer/lib/fetch/index.js
[ng] Module not found: Error: Can't resolve 'https' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\fetch'
[ng] ERROR in ./node_modules/nodemailer/lib/mailer/index.js
[ng] Module not found: Error: Can't resolve 'net' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\mailer'
[ng] ERROR in ./node_modules/nodemailer/lib/shared/index.js
[ng] Module not found: Error: Can't resolve 'net' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\shared'
[ng] ERROR in ./node_modules/nodemailer/lib/smtp-connection/http-proxy-client.js
[ng] Module not found: Error: Can't resolve 'net' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\smtp-connection'
[ng] ERROR in ./node_modules/nodemailer/lib/smtp-connection/index.js
[ng] Module not found: Error: Can't resolve 'net' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\smtp-connection'
[ng] ERROR in ./node_modules/nodemailer/lib/mime-node/index.js
[ng] Module not found: Error: Can't resolve 'os' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\mime-node'
[ng] ERROR in ./node_modules/nodemailer/lib/smtp-connection/index.js
[ng] Module not found: Error: Can't resolve 'os' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\smtp-connection'
[ng] ERROR in ./node_modules/nodemailer/lib/dkim/index.js
[ng] Module not found: Error: Can't resolve 'path' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\dkim'
[ng] ERROR in ./node_modules/nodemailer/lib/mime-funcs/mime-types.js
[ng] Module not found: Error: Can't resolve 'path' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\mime-funcs'
[ng] ERROR in ./node_modules/nodemailer/lib/base64/index.js
[ng] Module not found: Error: Can't resolve 'stream' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\base64'
[ng] ERROR in ./node_modules/nodemailer/lib/dkim/index.js
[ng] Module not found: Error: Can't resolve 'stream' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\dkim'
[ng] ERROR in ./node_modules/nodemailer/lib/dkim/message-parser.js
[ng] Module not found: Error: Can't resolve 'stream' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\dkim'
[ng] ERROR in ./node_modules/nodemailer/lib/dkim/relaxed-body.js
[ng] Module not found: Error: Can't resolve 'stream' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\dkim'
[ng] ERROR in ./node_modules/nodemailer/lib/fetch/index.js
[ng] Module not found: Error: Can't resolve 'stream' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\fetch'
[ng] ERROR in ./node_modules/nodemailer/lib/mime-node/index.js
[ng] Module not found: Error: Can't resolve 'stream' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\mime-node'
[ng] ERROR in ./node_modules/nodemailer/lib/mime-node/last-newline.js
[ng] Module not found: Error: Can't resolve 'stream' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\mime-node'
[ng] ERROR in ./node_modules/nodemailer/lib/qp/index.js
[ng] Module not found: Error: Can't resolve 'stream' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\qp'
[ng] ERROR in ./node_modules/nodemailer/lib/sendmail-transport/le-unix.js
[ng] Module not found: Error: Can't resolve 'stream' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\sendmail-transport'
[ng] ERROR in ./node_modules/nodemailer/lib/sendmail-transport/le-windows.js
[ng] Module not found: Error: Can't resolve 'stream' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\sendmail-transport'
[ng] ERROR in ./node_modules/nodemailer/lib/smtp-connection/index.js
[ng] Module not found: Error: Can't resolve 'stream' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\smtp-connection'
[ng] ERROR in ./node_modules/nodemailer/lib/smtp-connection/data-stream.js
[ng] Module not found: Error: Can't resolve 'stream' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\smtp-connection'
[ng] ERROR in ./node_modules/nodemailer/lib/xoauth2/index.js
[ng] Module not found: Error: Can't resolve 'stream' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\xoauth2'
[ng] ERROR in ./node_modules/nodemailer/lib/smtp-connection/http-proxy-client.js
[ng] Module not found: Error: Can't resolve 'tls' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\smtp-connection'
[ng] ERROR in ./node_modules/nodemailer/lib/smtp-connection/index.js
[ng] Module not found: Error: Can't resolve 'tls' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\smtp-connection'
[ng] ERROR in ./node_modules/nodemailer/lib/fetch/index.js
[ng] Module not found: Error: Can't resolve 'zlib' in 'C:\Users\Admin\Desktop\ionic\ionic-project-1-login-and-nested-list-and-report-modal-master\node_modules\nodemailer\lib\fetch'
[ng] chunk {common} common.js, common.js.map (common) 26.2 kB  [rendered]
[ng] chunk {focus-visible-15ada7f7-js} focus-visible-15ada7f7-js.js, focus-visible-15ada7f7-js.js.map (focus-visible-15ada7f7-js) 2.11 kB  [rendered]
[ng] chunk {input-shims-d4609e0d-js} input-shims-d4609e0d-js.js, input-shims-d4609e0d-js.js.map (input-shims-d4609e0d-js) 15.3 kB  [rendered]
[ng] chunk {keyboard-a2b5e51d-js} keyboard-a2b5e51d-js.js, keyboard-a2b5e51d-js.js.map (keyboard-a2b5e51d-js) 7.28 kB  [rendered]
[ng] chunk {login-login-module} login-login-module.js, login-login-module.js.map (login-login-module) 14 kB  [rendered] 
[ng] chunk {main} main.js, main.js.map (main) 25.9 kB [initial] [rendered]
[ng] chunk {polyfills} polyfills.js, polyfills.js.map (polyfills) 268 kB [initial] [rendered]
[ng] chunk {polyfills-core-js} polyfills-core-js.js, polyfills-core-js.js.map (polyfills-core-js) 78.7 kB  [rendered]   
[ng] chunk {polyfills-css-shim} polyfills-css-shim.js, polyfills-css-shim.js.map (polyfills-css-shim) 10.5 kB  [rendered]
[ng] chunk {polyfills-dom} polyfills-dom.js, polyfills-dom.js.map (polyfills-dom) 38.5 kB  [rendered]
[ng] chunk {product-list-product-list-module} product-list-product-list-module.js, product-list-product-list-module.js.map (product-list-product-list-module) 17.4 kB  [rendered]
[ng] chunk {reset-password-one-reset-password-one-module} reset-password-one-reset-password-one-module.js, reset-password-one-reset-password-one-module.js.map (reset-password-one-reset-password-one-module) 489 kB  [rendered]
[ng] chunk {runtime} runtime.js, runtime.js.map (runtime) 9.82 kB [entry] [rendered]
[ng] chunk {shadow-css-7f9c6860-js} shadow-css-7f9c6860-js.js, shadow-css-7f9c6860-js.js.map (shadow-css-7f9c6860-js) 15.9 kB  [rendered]
[ng] chunk {signup-signup-module} signup-signup-module.js, signup-signup-module.js.map (signup-signup-module) 16 kB  [rendered]
[ng] chunk {status-tap-171795fe-js} status-tap-171795fe-js.js, status-tap-171795fe-js.js.map (status-tap-171795fe-js) 1.6 kB  [rendered]
[ng] chunk {stock-modal-one-stock-modal-one-module} stock-modal-one-stock-modal-one-module.js, stock-modal-one-stock-modal-one-module.js.map (stock-modal-one-stock-modal-one-module) 4.55 kB  [rendered]
[ng] chunk {styles} styles.js, styles.js.map (styles) 100 kB [initial] [rendered]
[ng] chunk {swipe-back-0a6a44c8-js} swipe-back-0a6a44c8-js.js, swipe-back-0a6a44c8-js.js.map (swipe-back-0a6a44c8-js) 3.05 kB  [rendered]
[ng] chunk {swiper-bundle-e8f38f34-js} swiper-bundle-e8f38f34-js.js, swiper-bundle-e8f38f34-js.js.map (swiper-bundle-e8f38f34-js) 196 kB  [rendered]
[ng] chunk {tap-click-252af35a-js} tap-click-252af35a-js.js, tap-click-252af35a-js.js.map (tap-click-252af35a-js) 6.22 kB  [rendered]
[ng] chunk {vendor} vendor.js, vendor.js.map (vendor) 5.25 MB [initial] [rendered]
[ng] Date: 2020-07-13T15:07:18.475Z - Hash: fd07469b5de5766cdb0b - Time: 14159ms

[INFO] Development server running!

       Local: http://localhost:8100

       Use Ctrl+C to quit this process

[INFO] Browser window opened to http://localhost:8100!

[ng] Error from chokidar (C:\): Error: EBUSY: resource busy or locked, lstat 'C:\DumpStack.log.tmp'
[ng] Error from chokidar (C:\): Error: EBUSY: resource busy or locked, lstat 'C:\hiberfil.sys'
[ng] Error from chokidar (C:\): Error: EBUSY: resource busy or locked, lstat 'C:\pagefile.sys'
[ng] Error from chokidar (C:\): Error: EBUSY: resource busy or locked, lstat 'C:\swapfile.sys'

我的代码(.ts)很简单

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

import * as nodemailer from 'nodemailer';
@Component({
  selector: 'app-reset-password-one',
  templateUrl: './reset-password-one.page.html',
  styleUrls: ['./reset-password-one.page.scss'],
})
export class ResetPasswordOnePage implements OnInit {

  emailTo = '';


  // create expiration date

  constructor() { }

  ngOnInit() {
  }

  sendVerification() {
    // this.getToken();
    this.sendMail();

  }

  getToken() {
    //create random 16 character token
    var chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    var token = '';
    for (var i = 16; i > 0; --i) {
      token += chars[Math.round(Math.random() * (chars.length - 1))];
    }
    console.log(token);
    return token;
  }

  sendMail() {

    var transporter = nodemailer.createTransport({
      service: 'gmail',
      auth: {
        user: 'youremail@gmail.com',
        pass: 'yourpassword'
      }
    });

    var mailOptions = {
      from: 'youremail@gmail.com',
      to: 'myfriend@yahoo.com',
      subject: 'Sending Email using Node.js',
      text: 'That was easy!'
    };

    transporter.sendMail(mailOptions, function (error, info) {
      if (error) {
        console.log(error);
      } else {
        console.log('Email sent: ' + info.response);
      }
    });

  }

}

永远行不通!由于 nodemailer 是服务器端 nodejs 模块,因此它不适用于浏览器中的 运行。该模块需要一堆连接对象才能正常工作,而浏览器不允许这样做。所以,最后,你应该有一个后端来处理这种东西。

除了浏览器的连接问题外,您还面临丢失 email service 凭据的高安全风险!因为 angular 是一个客户端框架,所以您的代码对包含您的电子邮件凭据的 客户端可见 !所以任何人都可以反编译你的代码并窃取你不想要的代码。

因此,请尝试将您的电子邮件逻辑移至后端并保持稳定和安全!