我尝试在firebase函数中运行puppeteer。据我了解 https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md#running-puppeteer-on-google-cloud-functions 应该可以添加
"engines": {
"node": "8"
},
在package.json中。我收到了使用Firebase deploy的积极反馈
functions: updating Node.js 8 function helloWorld(us-central1)...
可悲的是
Error: Error: Failed to launch chrome!
[0408/080847.149912:ERROR:zygote_host_impl_linux.cc(89)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
TROUBLESHOOTING: https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md
at startPuppeteer.then.then.then.catch.error (/srv/index.js:42:15)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:229:7)
在本地工作了相同的代码。
那是失败的云功能代码。
const functions = require('firebase-functions');
const puppeteer = require('puppeteer');
let browser = ""
let page = ""
const startPuppeteer = async () => {
browser = await puppeteer.launch();
page = await browser.newPage()
}
const usePageInPuppeteer = async (url) => {
await page.goto(url);
return await page.title()
}
const closePuppeteer = async () => {
return await browser.close();
}
const runtimeOpts = {
timeoutSeconds: 500,
memory: '2GB'
}
exports.helloWorld = functions
.runWith(runtimeOpts)
.https.onRequest((request, response) => {
//response.send()
startPuppeteer()
.then(() => {
return usePageInPuppeteer('https://www.google.com')
})
.then(returnUse => {
console.log(returnUse)
return response.send(returnUse)
})
.then(() => {
return closePuppeteer()
})
.catch(error => {
throw new Error(error)
});
});
该本地测试有效
const puppeteer = require('puppeteer');
let browser = ""
let page = ""
const startPuppeteer = async () => {
browser = await puppeteer.launch();
page = await browser.newPage()
}
const usePageInPuppeteer = async (url) => {
await page.goto(url);
return await page.title()
}
const closePuppeteer = async () => {
return await browser.close();
}
startPuppeteer()
.then(() => {
return usePageInPuppeteer('https://www.google.com')
})
.then(returnUse => {
console.log(returnUse)
return closePuppeteer()
})
.catch(error => {
throw new Error(error)
});
答案 0 :(得分:3)
const startPuppeteer = async () => {
browser = await puppeteer.launch({args: ['--no-sandbox', '--disable-setuid-sandbox']});
page = await browser.newPage()
}
答案 1 :(得分:0)
您需要以沙盒模式启动chrome。不直接支持以根用户身份运行chrome,您可以传递参数email
以root用户身份启动它。
最终代码如下所示
--no-sandbox