错误:运行firebase云函数时“找不到模块”

时间:2022-04-05 20:20:09
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.

我正在用我的Firebase app发邮件,我使用的是Firebase cloud函数,按照我在使用Firebase web app发邮件时问的问题发送邮件,上面的代码是我的索引。js文件。

And this is my package.json file

这是我的包裹。json文件

{
  "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? 错误:运行firebase云函数时“找不到模块”

但是在部署代码时,我得到了一个错误。你把包里所有需要的模块都列出来了吗?json依赖性?这是什么错误。如何解决这个问题?

1 个解决方案

#1


3  

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

您的依赖项中缺少了nodemailer。只是把它…

npm install nodemailer --save

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

将导致(x.x)x是合适的版本)

"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

这很可能适用于您的开发,因为您实际上已经在本地或全局安装了nodemailer,但是正如错误指出的那样,它在远程机器上是不存在的

Cannot find module 'nodemailer'

找不到模块“nodemailer”

#1


3  

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

您的依赖项中缺少了nodemailer。只是把它…

npm install nodemailer --save

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

将导致(x.x)x是合适的版本)

"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

这很可能适用于您的开发,因为您实际上已经在本地或全局安装了nodemailer,但是正如错误指出的那样,它在远程机器上是不存在的

Cannot find module 'nodemailer'

找不到模块“nodemailer”