Closed jacksonkasi1 closed 2 weeks ago
same issue
Hi @Merynek, Let me know if there’s anything else you think could be worth trying in the meantime.
I tried it in localhost outside of the serverless and command "chromium.executablePath()" works. But on launch puppeteer i got this error: "Failed to launch the browser process! spawn C:\Users\merav\AppData\Local\Temp\chromium ENOENT" So mby it would be same issue on serverless. So I have 2 issues :-D
Hi @Merynek, I’ve raised the same issue over at Puppeteer. You can check the details here: Puppeteer Issue #13069.
The Chromium binary is now launching, but I’m currently facing an issue with Puppeteer itself. Would appreciate any insights you might have!
Hey @jacksonkasi1 👋
I just ran into the same issue. This looks to be the case because we are bundling the code and this project doesn't support that by default (according to https://github.com/Sparticuz/chromium/blob/master/source/index.ts#L320).
I worked around this by explicitly setting where the Chromium Lambda Layer files get overlayed:
await chromium.executablePath("/opt/nodejs/node_modules/@sparticuz/chromium/bin")
Hi @DAcodedBEAT,
I followed your guidance and attempted to set the executable path to the Chromium binary within /opt/nodejs/node_modules/@sparticuz/chromium/bin
. Below are the key steps I took and the corresponding errors:
// @ts-ignore
import chromium from "@sparticuz/chromium";
import puppeteer from "puppeteer-core";
import { readdirSync } from "fs";
export const getChrome = async (url: string) => {
let browser = null;
try {
console.log("Launching Chrome in serverless environment...");
// Log available directories
const availableDirs = readdirSync("/opt/nodejs/node_modules/@sparticuz/chromium");
console.log("Available directories in /opt/nodejs/node_modules/@sparticuz/chromium:", availableDirs);
// List files in bin directory
const binFiles = readdirSync("/opt/nodejs/node_modules/@sparticuz/chromium/bin");
console.log("Files in /opt/nodejs/node_modules/@sparticuz/chromium/bin:", binFiles);
// Set executable path to Chromium
const executablePath = "/opt/nodejs/node_modules/@sparticuz/chromium/bin";
console.log(`🚀 Executable path: ${executablePath}`);
browser = await puppeteer.launch({
executablePath,
headless: true,
args: chromium.args,
});
const page = await browser.newPage();
await page.goto(url, { waitUntil: 'networkidle0' });
return { browser, page };
} catch (error) {
console.error("Error launching Chrome:", error);
throw error;
} finally {
if (browser) await browser.close();
}
};
First Attempt (setting path to /opt/nodejs/node_modules/@sparticuz/chromium/bin/chromium
):
ERROR Error launching Chrome: Error: Browser was not found at the configured executablePath (/opt/nodejs/node_modules/@sparticuz/chromium/bin/chromium)
It seems the Chromium binary was not found at the specified path.
Second Attempt (setting path to /opt/nodejs/node_modules/@sparticuz/chromium/bin
):
ERROR Error launching Chrome: Error: Failed to launch the browser process! spawn /opt/nodejs/node_modules/@sparticuz/chromium/bin EACCES
This indicates a permission issue (EACCES
), likely due to the binary needing to be extracted or inflated before it can be used.
Here’s what I found during logging:
INFO Available directories in /opt/nodejs/node_modules/@sparticuz/chromium: [ 'bin', 'build', 'package.json' ]
INFO Files in /opt/nodejs/node_modules/@sparticuz/chromium/bin: [ 'al2.tar.br', 'al2023.tar.br', 'chromium.br', 'fonts.tar.br', 'swiftshader.tar.br' ]
It seems the binary is still in compressed format (chromium.br
), which may explain why it’s not launching properly.
Would appreciate any advice on how to properly extract the .br
files or inflate them for use in Lambda. Thanks!
You need to await chromium.executablePath(LOCATION_OF_BR_FILE)
instead of calling the path in puppeteer.
Hi @Sparticuz,
Thanks for the guidance! I updated the code as per your suggestion by using await chromium.executablePath(executablePath)
instead of directly passing the path. Here's the updated snippet:
// Set the executable path to the chromium binary inside the bin directory
const executablePath = "/opt/nodejs/node_modules/@sparticuz/chromium/bin";
console.log(`🚀 Executable path: ${executablePath}`);
browser = await puppeteer.launch({
executablePath: await chromium.executablePath(executablePath),
headless: true,
args: chromium.args,
});
const page = await browser.newPage();
await page.goto(url, { waitUntil: 'networkidle0' });
However, I'm still encountering the same issue that I previously raised on Puppeteer (Puppeteer Issue #13069).
INFO Files in /opt/nodejs/node_modules/@sparticuz/chromium/bin: [ 'al2.tar.br', 'al2023.tar.br', 'chromium.br', 'fonts.tar.br', 'swiftshader.tar.br' ]
INFO 🚀 Executable path: /opt/nodejs/node_modules/@sparticuz/chromium/bin
ERROR Error capturing screenshot: TargetCloseError: Protocol error (Page.bringToFront): Session closed. Most likely the page has been closed.
ERROR Error: Protocol error: Connection closed. Most likely the page has been closed.
It seems that Puppeteer is not able to keep the page open and the session is closing prematurely. The Chromium binary is there in .br
format, but the page brings up the error TargetCloseError: Session closed. Most likely the page has been closed
.
I would appreciate any further guidance on how I can resolve this.
Thanks!
Also, I have attached a log output CSV file which shows that all logs were captured. log-events-viewer-result.csv
Hey @Sparticuz, @DAcodedBEAT, and @Merynek 👋,
Thank you all for your help and time. I’ve resolved the issue – it turned out to be a configuration mistake on my end, and there’s no problem with the library itself.
For anyone who may face a similar issue, I’ve set up a simple code configuration to use Chrome in a Lambda serverless environment. I’ve also added the necessary steps and included a script to automate setting up the Chromium Lambda Layer.
You can check out the full setup here: aws-lambda-chromium-screenshot-api.
Hopefully, this will be helpful for others facing the same challenge.
I'll go ahead and close this issue. Thanks again!
I am using
@sparticuz/chromium
withpuppeteer-core
in an AWS Lambda function to capture screenshots. While running in the serverless environment (AWS Lambda), Chromium fails to launch and throws the following error:The issue occurs when attempting to resolve the executable path for Chromium, as provided by
@sparticuz/chromium
. It seems the Lambda layer isn't correctly resolving or finding the binary in the expected location.Steps to Reproduce
puppeteer-core
and@sparticuz/chromium
.Code Snippets
Environment
20.x
127.0.0
21.5.0
Expected Behavior
Chromium should launch without errors in the AWS Lambda environment, and I should be able to take screenshots of the provided URLs using
puppeteer-core
and@sparticuz/chromium
.Actual Behavior
Chromium fails to launch, and I get the following error:
Troubleshooting Steps Taken
chromium.executablePath()
and found that it's resolving tonull
or pointing to a non-existent directory (/var/task/bin
)./var/task/bin/chromium
, but the error persists.x86_64
architecture.Possible Cause
It seems like the Chromium binary path is not correctly resolved in the Lambda environment, possibly due to the way the Lambda layer is set up or how
chromium.executablePath()
is being used in this specific environment.Logs
Request for Help
Could this be a configuration issue with the Lambda layer or an incorrect way of resolving the Chromium executable path? Any guidance on how to resolve this would be greatly appreciated.