I've been working on a gcloud function with React and Puppeteer. My team and I have come across this problem we can't seem to solve. We've looked almost everywhere.
Cloud function
index.js
const functions = require("firebase-functions");
const puppeteer = require("puppeteer");
exports.searchFor = functions.runWith({memory: "1GB"}).https.onCall(async (data, context) => {
try {
const browser = await puppeteer.launch({headless: true, args: [
'--no-sandbox',
'--disable-setuid-sandbox',
]})
} catch (error) {
return error
}
})
Client
import firebase from 'firebase/app'
import "firebase/functions";
import {functions} from "../firebase";
import {parse} from "query-string";
import {RouteComponentProps} from "react-router";
import {searchQuery} from "../functions/tasks"
import {withRouter} from 'react-router-dom';
import React from "react";
// with proper values, of course
const firebaseConfig = {
apiKey: "",
authDomain: "",
databaseURL: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: "",
measurementId: "",
}
const app = firebase.initializeApp(firebaseConfig);
const searchFor = firebase.functions(app).httpsCallable("searchFor")
export const Search = withRouter(props => <Searcher {...props}/>);
class Searcher extends React.Component<React.PropsWithChildren<RouteComponentProps<any>>> {
private location = this.props.location;
private searchQuery = parse(this.location.search)
private testSearch = async (): void => {
return await searchFor({data: "test"}).then((res) => {
return res.data
})
}
public componentDidMount = (): void => {
this.testSearch().then((out) => {
console.log(out)
})
}
public render = (): JSX.Element => {
return (
<div id="search">
</div>
);
}
}
Output
In the cloud function, await puppeteer.launch() throws an error. In the console, there is simply an empty object. And if we don't use a try catch block in the cloud function, the console outputs error INTERNAL and the status turns to 500. We're using Node JS 10 for our engine
tl;dr
const browser = await puppeteer.launch({headless: true, args: [
'--no-sandbox',
'--disable-setuid-sandbox',
]})
In a Google cloud function is throwing an unknown error and we don't know how to fix it.