puppeteer / puppeteer

JavaScript API for Chrome and Firefox
https://pptr.dev
Apache License 2.0
88.3k stars 9.07k forks source link

[Bug]: SyntaxError: Unexpected token '.' in EventEmitter.js L#163 #10509

Closed sambhav2612 closed 1 year ago

sambhav2612 commented 1 year ago

Minimal, reproducible example

// in NestJS

import { Injectable, Logger } from '@nestjs/common';
import { S3Service } from './s3.service';
import * as puppeteer from 'puppeteer';

@Injectable()
export class PdfService {
  private browser: puppeteer.Browser;
  private readonly logger = new Logger(PdfService.name);
  constructor(private readonly s3Service: S3Service) {}
  async initialize(): Promise<void> {
    this.browser = await puppeteer.launch({ headless: 'new' });
    this.logger.log(`Browser instance initialized`);
  }

  async close(): Promise<void> {
    await this.browser.close();
    this.logger.log(`Browser instance closed`);
  }

  async generatePdf(
    reportId: string,
    orgId: string,
    htmlContent: string,
  ): Promise<any> {
    await this.initialize();
    const page = await this.browser.newPage();
    await page.setContent(htmlContent, { waitUntil: 'networkidle0' });
    const pdfContent = await page.pdf({ format: 'A4', printBackground: true });
    const res = await this.s3Service.uploadReports(reportId, orgId, pdfContent);
    this.close();
    return res;
  }
}

Error string

SyntaxError: Unexpected token '.' in EventEmitter.js

Bug behavior

Background

Convert handlebars HTML to PDF

Expectation

Worked on local env, worked in staging server, failed in prod server with same config as staging:

NestJS 7 Node 16.20.0 NPM 8.5.5 Ubuntu 18.04

Only local env runs on macOS but has same configs otherwise

Reality

Installation and build works fine but while running it fails with this error:

image

Puppeteer configuration

{ headless: 'new' }

Puppeteer version

20.7.4

Node version

16.20.0

Package manager

npm

Package manager version

8.5.5

Operating system

Linux

github-actions[bot] commented 1 year ago

The issue has been labeled as confirmed by the automatic analyser. Someone from the Puppeteer team will take a look soon!


Analyzer run

Lightning00Blade commented 1 year ago

Please provide a reproducible isolated example or a full repo where error is observable. Also are production builds failing locally?

sambhav2612 commented 1 year ago

Also are production builds failing locally?

Builds are not failing on any machine, however this only happens on production while starting the node server

OrKoN commented 1 year ago

@sambhav2612 are you sure the production server runs node 16? and not node 14 or lower? The error looks like the syntax is not supported by your node version.

sambhav2612 commented 1 year ago

@sambhav2612 are you sure the production server runs node 16? and not node 14 or lower? The error looks like the syntax is not supported by your node version.

@OrKoN Here's a screengrab from the server:

image

Checking further we found our staging is running on Ubuntu v20.04, however, I don't think that will affect this.

We also installed system-level dependencies before running the server:

sudo apt-get install -y gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget libgbm-dev
OrKoN commented 1 year ago

could you add a basic Puppeteer script to the server and run it using this node version? perhaps there are multiple installed and the one used by pm2 is different?

SirLouen commented 1 year ago

could you add a basic Puppeteer script to the server and run it using this node version? perhaps there are multiple installed and the one used by pm2 is different?

I can confirm this also happens to me

# node test.js
/root/node_modules/puppeteer-core/lib/cjs/puppeteer/common/EventEmitter.js:129
        return this.eventsMap.get(event)?.length || 0;
                                         ^

SyntaxError: Unexpected token '.'
    at wrapSafe (internal/modules/cjs/loader.js:915:16)
    at Module._compile (internal/modules/cjs/loader.js:963:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/root/node_modules/puppeteer-core/lib/cjs/puppeteer/api/Browser.js:19:27)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)

I've run inside a very simple ubuntu:latest docker container (Yes, I could create a Dockerfile for this, but just to ease out the idea)

apt update
apt install nodejs npm
cd /root/
npm install puppeteer

And then run any puppeteer script and the error pops. Versions are wholly different from the ones of @sambhav2612, but the issue is the same

OrKoN commented 1 year ago

This error indicates that an old Node version (< 16) is used.

husseinpenart commented 1 year ago

while im using node v 16> but im getting same error in cpanel

arazgholami commented 11 months ago

Minimal, reproducible example

// in NestJS

import { Injectable, Logger } from '@nestjs/common';
import { S3Service } from './s3.service';
import * as puppeteer from 'puppeteer';

@Injectable()
export class PdfService {
  private browser: puppeteer.Browser;
  private readonly logger = new Logger(PdfService.name);
  constructor(private readonly s3Service: S3Service) {}
  async initialize(): Promise<void> {
    this.browser = await puppeteer.launch({ headless: 'new' });
    this.logger.log(`Browser instance initialized`);
  }

  async close(): Promise<void> {
    await this.browser.close();
    this.logger.log(`Browser instance closed`);
  }

  async generatePdf(
    reportId: string,
    orgId: string,
    htmlContent: string,
  ): Promise<any> {
    await this.initialize();
    const page = await this.browser.newPage();
    await page.setContent(htmlContent, { waitUntil: 'networkidle0' });
    const pdfContent = await page.pdf({ format: 'A4', printBackground: true });
    const res = await this.s3Service.uploadReports(reportId, orgId, pdfContent);
    this.close();
    return res;
  }
}

Error string

SyntaxError: Unexpected token '.' in EventEmitter.js

Bug behavior

  • [ ] Flaky
  • [x] PDF

Background

Convert handlebars HTML to PDF

Expectation

Worked on local env, worked in staging server, failed in prod server with same config as staging:

NestJS 7 Node 16.20.0 NPM 8.5.5 Ubuntu 18.04

Only local env runs on macOS but has same configs otherwise

Reality

Installation and build works fine but while running it fails with this error:

image

Puppeteer configuration

{ headless: 'new' }

Puppeteer version

20.7.4

Node version

16.20.0

Package manager

npm

Package manager version

8.5.5

Operating system

Linux

Just idea: Make sure you're using updated version of node, NOT with running node -v inside cli but inside your code. In my case, php exec('node -v') returned version 12 regarding that node -v inside cli returned 23. You can use n (npm install -g n and n stable) to update your node version. Don't forget to restart your webserver.