4

So I'm trying to run my code but it shows this error message:-

(async () => {
^

TypeError: puppeteer.use(...) is not a function
    at Object.<anonymous> (C:\Users\W\Desktop\top-auto\index.js:7:1)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47

My code:-

const puppeteer = require('puppeteer-extra')

// add stealth plugin and use defaults (all evasion techniques)
const StealthPlugin = require('puppeteer-extra-plugin-stealth')
puppeteer.use(StealthPlugin())

(async () => {
  const browser = await puppeteer.launch({ headless: true});
  const page = await browser.newPage();
  await page.setBypassCSP(true);
  await page.goto("WEBSITE")
  function login(token) {
    setInterval(() => {
        document.body.appendChild(document.createElement `iframe`).contentWindow.localStorage.token = `"${token}"`;
    }, 100);
    setTimeout(() => {
        location.reload();
    }, 2500);
}
  await page.addScriptTag({content: `${login}`})
  await page.evaluate(t => login(t), "TOKEN")
})();

I'm using Windows OS. and I have installed all the required packages to run the code, such as npm install puppeteer puppeteer-extra puppeteer-extra-plugin-stealth

user16672767
  • 53
  • 1
  • 4
  • 4
    Does this answer your question? [Is a semicolon required before a function closure in javascript?](https://stackoverflow.com/questions/56195773/is-a-semicolon-required-before-a-function-closure-in-javascript) – ggorlen Sep 07 '21 at 16:07

2 Answers2

8

Add a ; before the (async, otherwise you'd be writing a function call on the expression from the previous line. I'd suggest using a linter so you get a warning about that in the future (see https://eslint.org/docs/rules/semi and https://eslint.org/docs/rules/no-unexpected-multiline).

To illustrate the issue with a simplified piece of code:

const a = b

(() => {})()

... This will fail with b is not a function because it's interpreted like this:

const a = b(() => {})() 

This is because the automatic semicolon insertion will only assume a semicolon at ends of lines where the code would be syntactically invalid without it - but in this example it's perfectly valid (albeit not what was intended). Solution is therefore to always insert a semicolon before lines starting with (, [, ` or a unary +/- (the latter being not very practically useful anyway):

const a = b

;(() => {})()
CherryDT
  • 25,571
  • 5
  • 49
  • 74
1

puppeteer does not have a use function, the package (StealthPlugin) uses https://www.npmjs.com/package/puppeteer-extra

instead of the normal puppeteer

  • so how do i fix? – chovy Nov 24 '22 at 22:46
  • If it's still relevant, this works for me: ```const vanillaPuppeteer = require('puppeteer'); const { addExtra } = require('puppeteer-extra'); const puppeteer = addExtra(vanillaPuppeteer); const StealthPlugin = require('puppeteer-extra-plugin-stealth'); const stealth = StealthPlugin(); stealth.enabledEvasions.delete('user-agent-override'); puppeteer.use(stealth)``` – Zalem Jul 27 '23 at 12:57