【问题标题】:Error: "Cannot find module" while running firebase cloud function错误:运行 firebase 云功能时出现“找不到模块”
【发布时间】:2018-06-09 02:10:47
【问题描述】:
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');
        }
    });
});

我正在从我的 Firebase 应用发送邮件。根据我在Sending email using Firebase web app 中提出的问题,我使用 Firebase 云功能发送邮件。上面的代码是我的 index.js 文件。

这是我的 package.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
}

但在部署代码时出现错误。 您是否在 package.json 依赖项中列出了所有必需的模块? 这是什么错误。如何解决?

【问题讨论】:

    标签: javascript node.js firebase google-cloud-functions nodemailer


    【解决方案1】:

    您的依赖项中缺少 nodemailer。只需添加它...

    npm install nodemailer --save
    

    将导致(其中x.x.x 是适当的版本)

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

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

    找不到模块“nodemailer”

    【讨论】:

    • 我应该插入为“nodemailer”:“^4.4.1”还是“nodemailer”:“~4.4.1”,如何找到它
    • @Badhusha 如果你愿意,你可以,但是当你 npm install nodemailer --save - --save 标志时,npm 会自动为你插入正确的版本。版本由作者控制,可在package.json for that project
    • @Badhusha 你解决了吗?我的回答对你有帮助吗?请让我知道,因为我想帮你解决这个问题。
    • 是的,它很有用。谢谢
    猜你喜欢
    • 1970-01-01
    • 2020-07-09
    • 2018-03-31
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2019-09-30
    • 1970-01-01
    相关资源
    最近更新 更多