Angular : Nodemailer shows a lot of runtime error

0

Issue

In my angular project, I want to use Nodemailer to send mail.

First problem is when I am trying to import ( i mean after doing npm i –save ) a lot of error occurs when i do ionic serve. I would like to repeat one thing : just after writing the import line, this big error log comes and app crashes like that

enter image description here

errors are most like this missing, that not found but why ? When I did npm i –save there wasn’t any error. Besides, I have no syntax error in my code . My only goal is to run the sendMail() function.

I just don’t get it what causes it? should i change nodemail to something else? please suggest.

Error log:

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'

My code (.ts) is very simple

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);
      }
    });

  }

}

Solution

It will never work! As nodemailer is a server-side nodejs module, it’s not meant for running inside your browser. The module require a bunch of connectivity objects to work properly which browser doesn’t allow it to do so. So, at the end, you should have a backend which handle this kind of stuff.

Apart from the connectivity issues by the browser, you’re at high security risk of loosing your email service credentials! Because angular is a client-side framework, your code is visible to the client that contains your email credentials! So anybody can decompile your code & steal which, I’m sure, you don’t want.

So, try moving your email logic to the backend & be stable as well as safe!

Answered By – Yogesh Aggarwal

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More