chrome-aws-lambda
chrome-aws-lambda copied to clipboard
[BUG] Error while using javascript
chrome-aws-lambda10.1.0:puppeteer/puppeteer-core13.6.0:- OS: Linux
- Node.js Version: 14
Expected Behavior
No error happening
Current Behavior
This error: {
"errorType": "Runtime.UserCodeSyntaxError",
"errorMessage": "SyntaxError: Invalid or unexpected token",
"trace": [
"Runtime.UserCodeSyntaxError: SyntaxError: Invalid or unexpected token",
" at _loadUserApp (/var/runtime/UserFunction.js:218:13)",
" at Object.module.exports.load (/var/runtime/UserFunction.js:279:17)",
" at Object.
Steps to Reproduce
async function launchBrowser() {
try {
const chromium = require('chrome-aws-lambda');
console.info('launching chrome-aws-lambda browser');
const browser = await chromium.puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless,
ignoreHTTPSErrors: true,
});
return browser;
} catch (e) {
console.log(e);
}
finally {
console.info('launching puppeteer browser');
}
}
async function lambdaHandler(event, context) {
console.info(`EVENT ${JSON.stringify(event, null, 2)}`);
const browser = await launchBrowser();
console.info('browser launched');
const page = await browser.newPage();
console.info('opened new tab');
let bodyText = '';
try {
await page.goto(event.url, {
waitUntil: 'networkidle0',
timeout: 10 * 1000,
});
console.info('page opened');
const stream = await page.pdf();
bodyText = stream.toString("base64");
console.info('pdf created');
} finally {
console.info('finally');
await page.close();
console.info('page closed');
await browser.close();
console.info('browser closed');
}
return {
statusCode: 200,
isBase64Encoded: true,
headers: { "Content-type": "application/pdf" },
body: bodyText
};
}
module.exports = { handler: lambdaHandler };
Dockerfile: FROM amazon/aws-lambda-nodejs:latest
COPY ./* ${LAMBDA_TASK_ROOT} RUN npm install --only=prod
CMD [ "index.handler" ]