Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a executable file using PKG (going wrong because of Node 20)

Tags:

javascript

I'm trying to transform this simple code into a executable, I didn't create any .json file, just the .js file.

It's my first time working with .JS, every time I ran pkg script.js it returns the error "Error! No available node version satisfies 'node20'".

Here's my code:

const puppeteer = require('puppeteer-extra');
const cliProgress = require('cli-progress');

const { DEFAULT_INTERCEPT_RESOLUTION_PRIORITY, Mouse } = require('puppeteer');
const AdblockerPlugin = require('puppeteer-extra-plugin-adblocker');
const progressBar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
const totalItens = 110;
puppeteer.use(
  AdblockerPlugin({
    // Optionally enable Cooperative Mode for several request interceptors
    interceptResolutionPriority: DEFAULT_INTERCEPT_RESOLUTION_PRIORITY
  })
);


function timeToMilliseconds(time) {
    const [minutes, seconds] = time.split(':').map(Number);
    const minutesInMilliseconds = minutes * 60 * 1000;
    const secondsInMilliseconds = seconds * 1000;
    const totalMilliseconds = minutesInMilliseconds + secondsInMilliseconds;
    return totalMilliseconds;
  }
(async () => {
    const browser = await puppeteer.launch({headless: 'new'});
    const page = await browser.newPage();
    await page.goto('https://www.justwatch.com/br/filme/besouro-azul');
    let title = await page.evaluate(() => {
        return document.getElementsByClassName("title-block")[0].textContent
    });
    console.log("TITULO: ", title)
    await page.click('[class="youtube-player__play-button youtube-player__play-button--backdrop"', 'left');
    progressBar.start(totalItens, 0)
    for (let i = 0; i <= totalItens; i++){
        progressBar.update(i)
        await page.waitForTimeout(1000);
        setInterval(() => {
        }, 1000);
    }
    title = title.split(' ')
    const filename = title[0] + '.png'
    await page.screenshot({path: filename});

    await browser.close();
    progressBar.stop();
})();

I was expecting to create a EXE file so I can share my code with friends.

like image 391
Confetti Avatar asked Oct 21 '25 06:10

Confetti


2 Answers

I haven't seen this fix anywhere else so though I'd chime in the fix that I've found:

pkg -t node*-win-x64 YOURFILE.js

Assuming this works on Linux and Mac too if you change the win bit respective to your OS, but I haven't tested it.

-t node*-win-x64 specifies which node version PKG should use, the key bit being node*; which works as a wildcard and tells PKG to use whatever node version is installed, therefore fixing the issue of it not liking/knowing your node version.

like image 70
P1kM1n Avatar answered Oct 22 '25 21:10

P1kM1n


Update your package.json to include the pkg configuration and specify a supported Node.js version, such as 14, 16, or 18.

"pkg": {
    "targets": [
      "node14",
      "node16",
      "node18"
    ]
  }
like image 41
Eldlabs Avatar answered Oct 22 '25 19:10

Eldlabs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!