npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

puppeteer-lambda

v1.1.3

Published

run puppeteer in lambda

Downloads

180

Readme

Puppeteer Lambda

Module for using Headless-Chrome by Puppeteer on AWS Lambda.
Idea from Puppeteer Lambda Starter Kit , thanks Taiki Sakamoto

How to use

npm install puppeteer-lambda
add --registry=https://registry.npm.taobao.org/ if you can't download the chromnium in China

(async () => {
    const puppeteerLambda = require('puppeteer-lambda');
    const browser = await puppeteerLambda.getBrowser({
    headless: true
    });
    const page = await browser.newPage();
    await page.goto('https://example.com');
    await page.screenshot({path: 'example.png'});

    await browser.close(); 
})();

NOTE: Suggest not to close browser in Lambda ENV, if close it , the Browser object is considered disposed and cannot be used anymore.

Packaging & Deploy

Lambda's memory needs to be set to at least 384 MB, but the more memory, the better the performance of any operations.

512MB -> goto(youtube): 6.481s
1536MB -> goto(youtube): 2.154s

You should also set a environment variable in lambda:

CUSTOM_CHROME = true

NOTE: This project uses puppeteer so don't forget to set PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true before run npm install when you prepare the package for lambda.

1.chrome in package (recommended)

run PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true CUSTOM_CHROME=true npm install puppeteer-lambda ,then deploy the package to lambda and set the following env variables in lambda.

  • CUSTOM_CHROME(required): tell the progress to use the custom chrome(locale version or download from s3 automatically)

node_modules/puppeteer-lambda should like:

puppeteer-lambda
│   README.md
│   ...    
│
└───chrome
│   │   headless_shell.tar.gz
│   
└───node_modules
    │   ...
│   
└───src
    │   ...
│   
└───test
    │   ...
    

2.chrome NOT in package

Due to the large size of Chrome, it may exceed the Lambda package size limit (50MB) depending on the other module to include. In that case, put Chrome Binary in S3 and download it at container startup so startup time will be longer.
You can also download the specific version of chrome from Serverless Chrome

Run PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true npm install puppeteer-lambda, deploy the package , and set following env valiables on Lambda.

  • CHROME_BUCKET(required): S3 bucket where Chrome is put
  • CHROME_KEY(optional): S3 key. default: headless_shell.tar.gz

How to Test

1. run test from your localhost

run npm run test

2 run in aws lambda simulation environment

test nodejs 8.10 npm run test-node8
test nodejs 10.x npm run test-node10

Q&A

Why not use puppeteer-core?

In development mode ,we still need chromnium for debugging , so better to puppeteer which will install chromnium automatically

How do we use puppeteer-lambda with TypeScript?

puppeteer-lambda type definitions depends on @types/puppeteer definition. You must add @types/puppeteer in your project.

npm install @types/puppeteer .

AWS Lambda Version .

now the prebuilt chromium v1.0.0-55 doesn't support AWS Lambda Nodejs version 10.x , if please use nodejs8.10 , if u prefer to use node10.x , please follow the instruction to build your own chromium and modify the configuration here NOTE: also please have a look this issue , seems lambda is changing their lambda environment, i tried built from amazonlinux 2 which is the base image for nodejs10x , but it still can not fund from lambda:nodejs10.x .