亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Azure Functions 中運行 Playwright

在 Azure Functions 中運行 Playwright

莫回無 2023-03-10 13:42:17
我正在嘗試運行一個簡單的 Azure 函數,該函數將根據瀏覽器中的可見內容進入頁面并生成 PDF。我創建了一個 NodeJS 12 Azure Function with Linux Consumption Plan (B1)。我設置PLAYWRIGHT_BROWSERS_PATH為0。函數代碼如下所示:const { chromium } = require("playwright-chromium");const baseUrl = "http://someurl";const targetPage = "/action/";module.exports = async function (context, req) {    const browser = await chromium.launch();    const page = await browser.newPage();    console.log (baseUrl + targetPage + context.bindingData.id)    await page.goto(baseUrl + targetPage + context.bindingData.id, { waitUntil: 'domcontentloaded' });        await page.emulateMedia({ media: 'print' });    onst result = await page.pdf({ printBackground: true, format: 'letter' });            context.res = {      body: result,      headers: { 'Content-Type': "application/pdf" }    };       await page.close();    await browser.close();};包.json{  "name": "",  "version": "",  "scripts": {    "start": "func start",    "test": "echo \"No tests yet...\""  },  "description": "",  "devDependencies": {},  "dependencies": {    "playwright-chromium": "1.3.0"  }}和函數.json{  "bindings": [    {      "authLevel": "anonymous",      "type": "httpTrigger",      "direction": "in",      "name": "req",      "methods": [        "get"      ],      "route": "route/{id}"    },    {      "type": "http",      "direction": "out",      "name": "res"    }  ]}當我檢查應用洞察時,我發現有以下錯誤:Exception while executing function: Functions.PDF Result: FailureException: Worker was unable to load function PDF: 'Error: Cannot find module 'playwright-chromium'Require stack:- /home/site/wwwroot/PDF/index.js- /azure-functions-host/workers/node/worker-bundle.js- /azure-functions-host/workers/node/dist/src/nodejsWorker.js'Stack: Error: Cannot find module 'playwright-chromium'
查看完整描述

1 回答

?
嚕嚕噠

TA貢獻1784條經驗 獲得超7個贊

我玩了一段時間你的例子,我得到了同樣的錯誤。這些是我發現使我的示例起作用的東西:

必須是 Linux。我知道您提到您選擇了一個 Linux 計劃。但我發現在 VS Code 中那部分是隱藏的,而在 Web 上默認是 Windows。這很重要,因為只有 Linux 計劃npm install在服務器上運行。

在此處輸入圖像描述

確保您在服務器上構建。您可以在 VS 代碼設置中找到此選項:

在此處輸入圖像描述

確保在發布之前設置了環境變量PLAYWRIGHT_BROWSERS_PATH

在此處輸入圖像描述


查看完整回答
反對 回復 2023-03-10
  • 1 回答
  • 0 關注
  • 204 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號