8
const functions = require('firebase-functions');
var nodemailer = require('nodemailer');
// const express=require('express');

var transporter=nodemailer.createTransport('smtps://username@gmail.com:password5@smtp.gmail.com');
exports.sendMail=functions.https.onRequest((req,res)=>{
    var mailOptions={
        to: 'receiver@gmail.com',
        subject: 'Test Mail',
        html: 'Testing with Node.js'
    }
    transporter.sendMail(mailOptions,function(err,response){
        if(err){
            res.send('Mail not sent');
        }
        else{
            res.send('Mail sent');
        }
    });
});

I am sending mail from my Firebase app. I use Firebase cloud functions for sending the mail as per the question I asked in Sending email using Firebase web app. The above code is my index.js file.

And this is my package.json file

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase serve --only functions",
    "shell": "firebase experimental:functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "dependencies": {
    "firebase-admin": "~5.4.2",
    "firebase-functions": "^0.7.1",
    "sendgrid": "^5.2.3"
  },
  "private": true
}

But while deploy the code I get an error. Did you list all required modules in the package.json dependencies? What is this error. How to solve it? Error

scniro
  • 16,844
  • 8
  • 62
  • 106
Badhusha
  • 195
  • 2
  • 20

2 Answers2

10

You're missing nodemailer from your dependencies. Just add it...

npm install nodemailer --save

will result in (where x.x.x is the appropriate version)

"dependencies": {
  "firebase-admin": "~5.4.2",
  "firebase-functions": "^0.7.1",
  "nodemailer": "^x.x.x",
  "sendgrid": "^5.2.3"
}

This is most likely working for your development because you actually have nodemailer either installed locally or globally, but it's absent on the remote machine as the error points out

Cannot find module 'nodemailer'

scniro
  • 16,844
  • 8
  • 62
  • 106
  • Should I insert as "nodemailer": "^4.4.1", or "nodemailer": "~4.4.1", how to find it – Badhusha Dec 28 '17 at 14:25
  • 1
    @Badhusha You can if you wish, however when you `npm install nodemailer --save` - the `--save` flag, npm will automatically insert the correct version for you. The version is controlled by the author, and can be found on the [package.json for that project](https://github.com/nodemailer/nodemailer/blob/master/package.json#L3) – scniro Dec 28 '17 at 14:26
  • 1
    @Badhusha Did you solve this? Was my answer able to help you? Please let me know as I'd like to help you resolve this. – scniro Dec 30 '17 at 18:19
  • Yes, it was useful. Thanks – Badhusha Dec 31 '17 at 03:52
1

My scenario is different its related to multiple package.json, I have two different package.json, one is global and another is for internal cloud functions. My mistake I installed the packages outside the cloud functions. The solution is, just to make sure to execute npm i modulename inside the correct directory.

Ras
  • 159
  • 2
  • 5