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

var transporter=nodemailer.createTransport('smtps://[email protected]:[email protected]');
exports.sendMail=functions.https.onRequest((req,res)=>{
    var mailOptions={
        to: '[email protected]',
        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

1

1 Answers

10
votes

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'