Packaging error with pkg tool when updating puppeteer from v1.20.0 to v24.0.0

I’m having trouble creating an executable with the pkg bundler after updating puppeteer. Everything worked perfectly with the old version but now I get errors.

My build setup:

"scripts": {
    "clean": "rm -rf dist",
    "bundle": "pkg index.js --targets node18-macos-arm64 --output dist/my-app"
}

Error output when running the build:

npm run bundle

[email protected]
(node:49060) [DEP0040] DeprecationWarning: The `punycode` module is deprecated
Warning Babel parse has failed: Unexpected token, expected "from" (1:12)
Warning Failed to make bytecode node18-arm64 for file /snapshot/puppeteer-projects/node_modules/typed-query-selector/shim.d.ts

I tried adding pkg configuration but it didn’t help:

"assets": [
  "*"
],
"scripts": [
  "node_modules/puppeteer/lib/*.js",
  "!node_modules/**/*.d.ts",
  "!../node_modules/**/*.d.ts"
]

The packaging worked fine with puppeteer v1.20.0 but fails with v24.0.0. Has anyone successfully packaged a newer version of puppeteer with pkg? What configuration changes are needed?

Been there. pkg breaks constantly with modern dependencies like newer puppeteer versions. TypeScript declarations and ES modules just don’t work with pkg’s bundling.

Don’t waste time fighting build tools - automate puppeteer differently. Skip packaging completely and run scripts through a proper automation platform.

I moved all my puppeteer stuff to cloud workflows. No version compatibility issues, no bundling errors, no cross-platform headaches. Just automation that works.

Deploys instantly and triggers from anywhere - no local executables needed. Way cleaner than forcing outdated bundlers to work with modern libraries.

Check it out: https://latenode.com

The Problem:

You’re having trouble creating an executable with the pkg bundler after updating Puppeteer. The build fails with errors related to TypeScript declaration files (.d.ts) and ES modules, which pkg doesn’t fully support. This worked previously with older Puppeteer versions, but newer versions introduce incompatibilities.

:thinking: Understanding the “Why” (The Root Cause):

pkg is a bundler designed to package Node.js applications into executables. However, it has limitations when handling modern JavaScript features, specifically TypeScript declaration files (.d.ts) and the ES module syntax used extensively in newer Puppeteer versions. The .d.ts files are type definitions for TypeScript, and while essential for development, they are not executable code. pkg attempts to process them, leading to parsing errors. Similarly, pkg’s handling of ES modules is not as robust as other bundlers. This incompatibility results in build failures when packaging applications that depend on recent Puppeteer releases.

:gear: Step-by-Step Guide:

  1. Use nexe for Bundling: Instead of pkg, use nexe, a more modern bundler that has better support for TypeScript and ES modules commonly found in current Puppeteer and other Node.js projects.

    • Install nexe: Open your terminal and run: npm install -g nexe
    • Build your application: Use the nexe command to bundle your application. This will take care of the TypeScript and ES module issues. Adapt the target parameters as needed for your operating system. Example:
    nexe index.js --target macos-arm64-18.17.0 --output dist/my-app
    

    Replace index.js with your main application file and adjust the --target option to match your system architecture (e.g., linux-x64, win-x64).

  2. (Alternative) Exclude TypeScript Declarations with pkg (Less Recommended): If you must use pkg, you can attempt to exclude the TypeScript definition files. This is a less robust solution, as it only masks the symptom and could cause runtime errors or unexpected behaviour.

    • Modify package.json: Add the following to your package.json file within the pkg section:
    "pkg": {
      "scripts": [
        "index.js" //List only the javascript files
      ],
      "assets": [],
      "ignore": [
        "!node_modules/**/*.d.ts",
        "!**/*.d.ts"
      ],
      "targets": ["node18-macos-arm64"],
      "outputPath": "dist"
    }
    
    • Run pkg: Try running your pkg command again. This will attempt to build the executable without processing the .d.ts files.

:mag: Common Pitfalls & What to Check Next:

  • nexe Target: Ensure that you’ve selected the correct --target option for nexe. Incorrect target parameters lead to an incompatible executable file that won’t run. Check the nexe documentation for the appropriate options for your operating system and Node.js version.
  • Runtime Errors: Even after successfully building the executable, you might encounter runtime errors due to missing dependencies or other issues. Carefully review the application logs after execution.
  • Outdated pkg: If you’re sticking with pkg, make sure you’re using the latest version. Older versions have known issues with TypeScript and ES modules. Running npm update -g pkg ensures that you have the most recent bug fixes.
  • Dependency Conflicts: Review your project’s dependencies for any conflicts or incompatibility. Older versions of supporting libraries may still be expecting to find old versions of puppeteer, which may introduce errors not initially apparent during build.

:speech_balloon: Still running into issues? Share your (sanitized) config files, the exact command you ran, and any other relevant details. The community is here to help!

Your problem is that pkg tries to parse TypeScript declaration files as JavaScript code. Newer puppeteer versions use ES module syntax that pkg’s parser can’t handle. I got around this by excluding all TypeScript definitions and modern module files in my pkg config. Add these exclusions to your package.json: {“pkg”: {“scripts”: “build/**/*.js”,“assets”: ,“targets”: [“node18-macos-arm64”],“outputPath”: “dist”}} Also try downgrading to pkg 5.7.0 if you’re using 5.8.1 - the newer versions have regression issues with TypeScript files. Before bundling, create a separate build directory with only transpiled JavaScript files and exclude the entire node_modules/.bin directory. This worked for me with puppeteer v22.x, though you might still hit runtime issues with Chrome binary detection.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.