Home > Mobile >  How to fix issue "Error: Cannot find module 'puppeteer'" when running "pkg
How to fix issue "Error: Cannot find module 'puppeteer'" when running "pkg

Time:10-10

so I have a puppeteer script and I want to make it a .exe file that includes node_modules within the .exe file.

index.js:

const fs = require("fs");
const puppeteer = require("puppeteer-extra");

const StealthPlugin = require("puppeteer-extra-plugin-stealth");
puppeteer.use(StealthPlugin());

code...

When I run pkg . it creates the .exe files but I have to run it besides the node_modules folder to be able to run it without having the error " Error: Cannot find module 'puppeteer'"

I also tried this on my package.json

  "pkg": {
    "assets": "node_modules/**/*.*"
  },

But it has been like an hour or so, and nothing has been created, it's like it has frozen.

Anyone can help? Thanks a lot!

CodePudding user response:

try typing in your console: npm install puppeteer

CodePudding user response:

const puppeteer = require("puppeteer-extra");
const isPkg = typeof process.pkg !== 'undefined';

const chromiumExecutablePath = isPkg 
    ? puppeteer.executablePath().replace(
        /^.*?\/node_modules\/puppeteer\/\.local-chromium/,
        path.join(path.dirname(process.execPath), '.local-chromium')
      ) : puppeteer.executablePath()
);
const browser = await puppeteer.launch({
   executablePath: chromiumExecutablePath,
   args: ["--start-maximized"],
   headless: false,
   defaultViewport: null
})

And to build use npm script like:

"build": "pkg src/index.js --public --targets node14-win-x64 --out-path dist"
  • Related