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

nestjs-puppeteer

v2.1.0

Published

Puppeteer module for NestJS

Downloads

3,105

Readme

nestjs-puppeteer

Puppeteer module for Nest framework (node.js)

npm npm GitHub semantic-release: angular
Puppeter Peer Dep NestJS Peer Dep

Headless Chrome provider for NestJS, enabling easy integration of Puppeteer into your application.

[!IMPORTANT] Chrome introduces the "New Headless" mode in version 112. Most of puppeteer-extra plugins are NOT COMPATIBLE with this mode. If you want to use puppeteer-extra plugins, you need to define headless: true in the module configuration.

You can read more about the new headless mode here.

Installation

To begin using it, we first install the required dependencies.

$ npm install --save nestjs-puppeteer puppeteer-extra puppeteer

Usage

Once the installation process is complete we can import the PuppeteerModule into the root AppModule

import { Module } from '@nestjs/common';
import { PuppeteerModule } from 'nestjs-puppeteer';

@Module({
  imports: [
    PuppeteerModule.forRoot({ headless: 'new' }),
  ],
})
export class AppModule {}

The forRoot() method supports all the configuration properties exposed by the PuppeteerNodeLaunchOptions object used by the puppeteer.launch() method. There are several extra configuration properties described below.

| Property | Description | | ------------- | ------------- | | name | Browser name | | plugins | An array of puppeteer-extra plugins | | isGlobal | Should the module be registered in the global context | | headless | new for the New Headless mode, or true/false |

Once this is done, the Puppeteer Browser instance will be available for injection in any of the providers in the application.

import { Browser } from 'puppeteer';

@Module({
  imports: [
    PuppeteerModule.forRoot({ headless: 'new' }),
  ],
})
export class AppModule {
  constructor(@InjectBrowser() private readonly browser: Browser) {}
}

If you used the name option when registering the module, you can inject the browser by name.

ForFeature

After module registration, we can register specific pages for injection.

import { Module } from '@nestjs/common';
import { PuppeteerModule } from 'nestjs-puppeteer';

@Module({
  imports: [
    PuppeteerModule.forRoot({ headless: 'new' }),
    PuppeteerModule.forFeature(['page1', 'page2']),
  ],
})
export class AppModule {}

Once this is done the Page instance will be available for injection in any of the providers in the module.

import { Page } from 'puppeteer';

@Module({
  imports: [
    PuppeteerModule.forRoot({ headless: 'new' }),
    PuppeteerModule.forFeature(['page1', 'page2']),
  ],
})
export class AppModule {
  constructor(@InjectPage('page1') private readonly page1: Page) {}
}

Async configuration

You may want to pass your repository module options asynchronously instead of statically. In this case, use the forRootAsync() method, which provides several ways to deal with async configuration.

One approach is to use a factory function:

PuppeteerModule.forRootAsync({
  useFactory: () => ({
    headless: false,
  }),
})

Our factory behaves like any other asynchronous provider (e.g., it can be async and it's able to inject dependencies through inject).

PuppeteerModule.forRootAsync({
  imports: [ConfigModule],
  useFactory: async (configService: ConfigService) => ({
    headless: configService.isHeadless,
  }),
  inject: [ConfigService],
})

Alternatively you can use the useClass syntax:

PuppeteerModule.forRootAsync({
  useClass: PuppeteerConfigService,
})

The construction above will instantiate PuppeteerConfigService inside PuppeteerModule and use it to provide an options object by calling createPuppeteerOptions().
Note that this means that the PuppeteerConfigService has to implement the PuppeteerOptionsFactory interface, as shown below:

@Injectable()
class PuppeteerConfigService implements PuppeteerOptionsFactory {
  createPuppeteerOptions(): PuppeteerNodeLaunchOptions {
    return {
      headless: 'new',
    };
  }
}