I was trying to run Puppeteer on an AWS Lambda function and got the following error.
Error: Error: Cannot find module '/app/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Browser'
This has happened on both locally and when I deploy it to AWS as well.
The code:
const chromium = require("chrome-aws-lambda");
const generateImg = async (reviewId) => {
let browser = null;
let result = null;
try {
const executablePath = await chromium.executablePath;
browser = await chromium.puppeteer.launch({
args: chromium.args,
executablePath: executablePath,
headless: chromium.headless,
});
const page = await browser.newPage();
await page.goto(
"https://www.example.com"
);
result = await page.screenshot();
} catch (error) {
console.error("Error:", error);
} finally {
if (browser !== null) {
await browser.close();
}
}
return result;
};
package.json:
{
"name": "app",
"version": "1.0.0",
"main": "index.js",
"author": "",
"license": "MIT",
"dependencies": {
"chrome-aws-lambda": "^10.1.0",
"handlebars": "^4.7.8",
"node-fetch": "2.7.0",
"puppeteer-core": "^21.1.1"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "sls offline --stage local"
},
"devDependencies": {}
}