Search code examples
node.jspuppeteer

puppeteer config import order causes an error


i am using puppeteer in a nodejs module on a macos 14.5

$ jq -r '.type' package.json
module

$ jq -r '.devDependencies.puppeteer' package.json
22.11.0

i have a basic boilerplate as follows

$ cat puppeteer.config.cjs
// empty config

$ cat test.js
import puppeteer from "puppeteer";
import puppeteerConfig from "./puppeteer.config.cjs";
console.log("i am a puppet");

but i get an error upon execution

$ node test.js
node:internal/assert:14
    throw new ERR_INTERNAL_ASSERTION(message);
          ^

Error [ERR_INTERNAL_ASSERTION]: This is caused by either a bug in Node.js or incorrect usage of Node.js internals.
Please open an issue with this stack trace at https://github.com/nodejs/node/issues

    at assert (node:internal/assert:14:11)
    at cjsLoader (node:internal/modules/esm/translators:347:7)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:297:7)
    at ModuleJob.run (node:internal/modules/esm/module_job:222:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:123:5) {
  code: 'ERR_INTERNAL_ASSERTION'
}

Node.js v20.14.0

if i change the order of imports, the error is gone

$ cat test.js
import puppeteerConfig from "./puppeteer.config.cjs";
import puppeteer from "puppeteer";
console.log("i am a puppet");

$ node test.js
i am a puppet

i cannot debug further and don't understand why this is happening and would appreciate your help.


Solution

  • the problem arises from the caching of modules in nodejs and in cosmiconfig, which is the module that puppeteer uses to read its configuration file

    $ npm view puppeteer dependencies.cosmiconfig
    9.0.0
    

    the issue can be bypassed by using cosmiconfig to read the configuration file rather than importing it directly. something as follows worked for me.

    import cosmiconfig from "cosmiconfig";
    const explorerSync = cosmiconfig.cosmiconfigSync("puppeteer");
    const { config } = explorerSync.load("./puppeteer.config.cjs");