需要外部包时,Firebase 功能不会部署

Firebase function will not deploy when requiring outside packages

我在部署网络抓取功能时遇到问题,不知道如何解决这个问题。

Index.js

const functions = require("firebase-functions");
const pup = require("puppeteer"); 


const WebsiteData = require('./schema');  
  

exports.scrape = functions
    .runWith({ memory: '1GB' })
    .pubsub.schedule('0 0 * * *')
    .onRun(async (context) => {
        const browser = await pup.launch({
            headless: true,
            args: ['--no-sandbox', '--disable-setuid-sandbox']
        });

        const page = await browser.newPage();

        await page.setUserAgent(
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36'
        )

        var pages = [
            {
                name: 'aave-protocol',
                fee:'0.09',
                url: 'https://aave.com',
            },
            {
                name: 'uniswap-v2',
                fee:'0.30',
                url: 'https://uniswap.org',
            },
            {
                name: 'dydx',
                fee:'0.00',
                url: 'https://dydx.exchange',
            },
            {
                name: 'dodo-bsc',
                fee:'0.00',
                url: 'https://dodoex.io',
            },
            {
                name: 'cream-finance',
                fee:'0.03',
                url: 'https://cream.finance',
            },
            {
                name: 'multiplier-finance',
                fee:'0.06',
                url: 'https://multiplier.finance',
            }
        
        ]
        var result = [];

        for (var each in pages) {
            await page.goto(`https://www.dapp.com/app/${pages[each].name}`, { waitUntil: 'networkidle0' })
        

            var users = await page.evaluate(() => {
                return document.querySelector('#app > div.app-container > div.root.dapp-detail > section.detail-page-outer > div > div.left-sec > div.stats-card > div.chart-section > div:nth-child(2) > div:nth-child(4) > span.value > span').innerText
            })
            var volume = await page.evaluate(() => {
                return document.querySelector('#app > div.app-container > div.root.dapp-detail > section.detail-page-outer > div > div.left-sec > div.stats-card > div.chart-section > div:nth-child(3) > div:nth-child(4) > span.value > span:nth-child(2)').innerText
        })

        var obj = { "name": `${pages[each].name}`, "nDUniqueUsers": `${users}`, "nDTransactions": `${volume}`, "fee":  `${pages[each].fee}`, "url": `${pages[each].url}`};
            result.push(obj);
        }


        
        await browser.close();

        

          const websiteMongo = new WebsiteData({ sites: result });
        
        await websiteMongo.save(function(err,data){
            if (err) {
                console.log(err);
                return null;
            }
        });    
        
        console.log("Done.")
        return null;

    });

该功能旨在使用 puppeteer 打开大约 5 个页面收集信息并将数据上传到 MongoDB 数据库。该代码在本地主机上运行良好,但是当我 运行 firebase 部署时,出现错误。这是错误消息:“加载用户代码时函数失败。这可能是由于用户代码中的错误。错误消息:错误:请检查您的函数日志以查看错误原因:https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging. Please visit https://cloud.google.com/functions/docs/troubleshooting for in-深度故障排除文档。"}"

我知道问题由这两行组成:

const pup = require("puppeteer"); 


const WebsiteData = require('./schema');  

当我注释掉这两行时,我就可以部署函数了。这是 schema.js:

的代码

var mongoose = require('mongoose')
mongoose.connect("URI");

var Schema = mongoose.Schema

var websiteData = new Schema({
    sites: [{
        name: {
            type: String,
            required : true,
        },
        nDUniqueUsers: {
            type: String,
            required : true,
        },
        nDTransactions: {
            type: String,
            required : true,
        }, fee: {
            type: String,
            required : true,
        }, url: {
            type: String,
            required : true,
        }

    }],
})

var WebsiteData = mongoose.model("SiteData", websiteData);

module.exports = WebsiteData

我不知道如何解决这个问题。任何帮助将不胜感激。

我怀疑您没有在与您的函数一起部署的 package.jsondependencies 部分中包含“puppeteer”。或者您可能无意中将其包含在 devDependencies 部分而不是 dependencies.

// package.json
{
  // ...
  "dependencies": {
    "puppeteer": "^13.5.1" // be sure you have this
  }
}