Unexpected syntax error in Puppeteer when deploying

I’m having trouble with my Puppeteer script when I deploy it. It works fine on my computer but fails on Render and Docker. Here’s a simplified version of my code:

import puppeteer from 'puppeteer';
import path from 'path';

async function createDocument(data) {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  
  await page.goto(`http://myapp.com/generate/${data.id}`);
  await page.setViewport({ width: 800, height: 600 });
  
  await page.pdf({
    path: `documents/${data.id}.pdf`,
    format: 'A4',
    printBackground: true,
    margin: { top: '1cm', right: '1cm', bottom: '1cm', left: '1cm' }
  });
  
  await browser.close();
}

The error I’m getting is:

SyntaxError: Unexpected token '??='
    at Loader.moduleStrategy (internal/modules/esm/translators.js:149:18)

What could be causing this error on deployment but not locally? How can I fix it?

This error typically occurs due to Node.js version incompatibility. The ‘??=’ operator is a newer feature that older Node versions don’t support. To resolve this, you should specify a more recent Node version in your deployment environment.

For Render, you can add a ‘engines’ field to your package.json:

"engines": {
  "node": ">=14.0.0"
}

For Docker, update your Dockerfile to use a newer Node image:

FROM node:14

Also, ensure all your dependencies are up-to-date. If the issue persists, consider using a transpiler like Babel to convert your code to an older JavaScript version for broader compatibility.

sounds like a node version problem mate. try updating ur node version in the deployment config. for render, add “engines”: {“node”: “>=14”} to package.json. for docker, use a newer base image like “FROM node:14”. that should fix it. good luck!

I’ve encountered a similar issue when deploying Puppeteer scripts. In my experience, the error is typically due to a version mismatch between your local Node.js and the one on your deployment platforms. The nullish coalescing assignment (??=) is a newer feature that may not be supported in older Node versions. I resolved this by specifying a newer Node version in my deployment configuration, such as by adding a .node-version file or updating package.json to include the appropriate module settings. Alternatively, using a transpiler like Babel might help ensure compatibility across environments.