Everything works fine locally, but when I try to run my script using puppeteer in github action, I get this:
file:///dist/index.js:49412
const puppeteerRootDirectory = pkg_dir_1.default.sync(__dirname);
^
ReferenceError: __dirname is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and '/dist/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
I've tried defining the __dirname
before importing puppeteer but haven't had any luck. e.g.
import {dirname} from "path";
globalThis.__dirname = dirname(import.meta.url);
import puppeteer from "puppeteer";
EDIT:
This appears to be an issue with compiling via ncc. I can repro locally when I run ncc run [my script]
. I've looked into how to define __dirname for modules but am still running into issues. I tried something like this and adding it at the top of my script before my import statement but it doesn't make a difference. Is there anywhere I can add it that it would be recognized by puppeteer?