📜  puppeteer heroku (1)

📅  最后修改于: 2023-12-03 14:45:41.754000             🧑  作者: Mango

Puppeteer Heroku

Introduction

If you are a developer who wants to automate your browser-based tasks, you probably need a tool that can do that efficiently and swiftly. Puppeteer is one such tool. It is a Node library maintained by the Google Chrome team, which provides a high-level API for headless (or non-headless) browsers.

Puppeteer Heroku is an extension of Puppeteer that enables you to run Puppeteer scripts on the Heroku platform. Heroku is a cloud-based platform as a service (PaaS) that enables developers to build, run, and operate applications entirely in the cloud.

Getting Started

To start using Puppeteer on Heroku, you need to have a Heroku account and a Node project with Puppeteer installed. Here are the steps to follow:

  1. Create a new Heroku app by accessing the Heroku Dashboard and clicking on the “New” button.

  2. Once the app is created, navigate to the “Deploy” tab, select your deployment method (GitHub, Heroku Git, or Container Registry), and connect it to your repository.

  3. Next, add a Puppeteer buildpack to your app. A buildpack is a set of scripts that prepare your app for deployment on Heroku. To add a Puppeteer buildpack, navigate to the “Settings” tab, click on the “Add buildpack” button, and enter https://github.com/jontewks/puppeteer-heroku-buildpack as the buildpack URL.

  4. Finally, create a .env file in your project with the following contents:

PUPPETEER_PRODUCT=chrome
PUPPETEER_ARGS=--no-sandbox

These environment variables tell Puppeteer to use Chrome as the underlying browser and disable the sandboxing feature that is enabled by default on Linux-based systems (which are the systems used by Heroku).

Running Puppeteer Scripts on Heroku

Once you have set up your Heroku app and your Puppeteer project, you are ready to start running Puppeteer scripts on Heroku. To do this, follow these steps:

  1. Create a new JavaScript file in your Puppeteer project that contains your Puppeteer script.

  2. Update the package.json file in your project to include a new script that runs your Puppeteer script. The script should look like this:

{
  "name": "my-puppeteer-project",
  "version": "1.0.0",
  "scripts": {
    "start": "node my-puppeteer-script.js"
  }
}

This script tells Node to execute your Puppeteer script when the start command is run.

  1. Commit your changes to your repository and push them to your Heroku app.

  2. Once your app has been deployed, you can run your Puppeteer script by running the following command in your terminal:

heroku run start

This command tells Heroku to start a new instance of your app and run the start script, which will in turn run your Puppeteer script.

Conclusion

Puppeteer Heroku is a powerful tool that enables developers to run Puppeteer scripts on the Heroku platform. By following the steps outlined in this guide, you can easily set up your Heroku app and start running your Puppeteer scripts in the cloud!