Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: "Cannot find module" while running firebase cloud function

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

like image 679
Badhusha Avatar asked Dec 28 '17 14:12

Badhusha


2 Answers

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'

like image 123
scniro Avatar answered Oct 18 '22 08:10

scniro


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.

like image 42
Ras Avatar answered Oct 18 '22 07:10

Ras