Skip to content

Deploying a Puppeteer App

Puppeteer is a Node.js library that provides a high-level API to control headless Chrome or Chromium browsers. It’s widely used for web scraping, automated testing, rendering, and browser automation tasks, making it a powerful tool for developers who need to interact with web pages programmatically.

This guide explains how to deploy a Puppeteer application to Klutch.sh, both with and without a Dockerfile. It also covers installation and provides sample code to get started.

Prerequisites

  • Node.js 18+
  • Git and GitHub account
  • Klutch.sh account

Getting Started: Install Puppeteer

  1. Create a new directory for your app and initialize npm:
    Terminal window
    mkdir my-puppeteer-app
    cd my-puppeteer-app
    npm init -y
  2. Install Puppeteer:
    Terminal window
    npm install puppeteer
  3. Create a basic Puppeteer script (index.js):
    const puppeteer = require('puppeteer');
    (async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto('https://example.com');
    const title = await page.title();
    console.log('Page title:', title);
    await browser.close();
    })();
  4. Add a start script in your package.json:
    "scripts": {
    "start": "node index.js"
    }
  5. Test locally:
    Terminal window
    npm start

Deploying Without a Dockerfile

  1. Push your Puppeteer app to a GitHub repository.
  2. Log in to Klutch.sh.
  3. Create a new project and give it a name.
  4. Create a new app:
    • Select your Puppeteer GitHub repository and branch
    • Set the port to route traffic (if your app exposes a web server, e.g., 3000)
    • Choose region, compute, number of instances, and add any environment variables
  5. Add a start script in your package.json as shown above.
  6. Click “Create” to deploy. Klutch.sh will build and deploy your app automatically.

Deploying With a Dockerfile

  1. Add a Dockerfile to your project root. Example:
    # Use official Node.js image with Chromium dependencies
    FROM node:18-bullseye-slim
    # Install Puppeteer dependencies
    RUN apt-get update && apt-get install -y \
    ca-certificates \
    fonts-liberation \
    libasound2 \
    libatk-bridge2.0-0 \
    libatk1.0-0 \
    libcups2 \
    libdrm2 \
    libgbm1 \
    libgtk-3-0 \
    libnspr4 \
    libnss3 \
    libx11-xcb1 \
    libxcomposite1 \
    libxdamage1 \
    libxrandr2 \
    xdg-utils \
    --no-install-recommends \
    && rm -rf /var/lib/apt/lists/*
    # Set working directory
    WORKDIR /app
    # Copy package files and install dependencies
    COPY package*.json ./
    RUN npm install --production
    # Copy app source
    COPY . .
    # Expose port (if your app exposes a web server)
    EXPOSE 3000
    # Start the app
    CMD ["npm", "start"]
  2. Push your code (with Dockerfile) to GitHub.
  3. In Klutch.sh, follow the same steps to create a project and app, but select the Dockerfile option when prompted.
  4. Set the service details and environment variables as needed.
  5. Click “Create” to deploy. Klutch.sh will build your Docker image and deploy your app.

Note: If your Puppeteer app exposes a web server, ensure it listens on the PORT environment variable as shown above.


Resources


Deploying to Klutch.sh is simple and flexible. Choose the method that best fits your workflow and project requirements.