Xetera / ghost-cursor

🖱️ Generate human-like mouse movements with puppeteer or on any 2D plane
MIT License
1.09k stars 118 forks source link

SyntaxError: Cannot use import statement outside a module #32

Closed davidlinketech closed 3 years ago

davidlinketech commented 3 years ago

Im trying to run this code: `import { createCursor } from "ghost-cursor" import puppeteer from "puppeteer"

const run = async (url) => { const selector = "#sign-up button" const browser = await puppeteer.launch({ headless: false }); const page = browser.newPage() const cursor = createCursor(page) await page.goto(url) await page.waitForSelector(selector) await cursor.click(selector) // shorthand for // await cursor.move(selector) // await cursor.click() }

run(https://www.nike.com/de/login);`

But I'm getting this error the whole time: (node:3984) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension. (Usenode --trace-warnings ...` to show where the warning was created) C:\Users\David\Desktop\learn_js-Kopie.js:1 import { createCursor } from "ghost-cursor" ^^^^^^

SyntaxError: Cannot use import statement outside a module at wrapSafe (internal/modules/cjs/loader.js:1001:16) at Module._compile (internal/modules/cjs/loader.js:1049:27) 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 `

davidlinketech commented 3 years ago

PS: I'm really new to Github, so sorry if the Question doesn't look that nice :/

Niek commented 3 years ago

The code you're trying to run in TypeScript, not JS/ES. You need to change the import statements to require().

davidlinketech commented 3 years ago

Oh forgot to close this by myself, but for those who are interested, this is what I have now:

const puppeteer = require('puppeteer-extra') const StealthPlugin = require('puppeteer-extra-plugin-stealth') puppeteer.use(StealthPlugin()) const ghostCursor = require('ghost-cursor')

const run = async (url) => { const selector = '#keepMeLoggedIn' const browser = await puppeteer.launch({ headless: false }); const page = await browser.newPage() const cursor = await ghostCursor.createCursor(page); await page.goto(url) await page.waitForSelector(selector) await cursor.click(selector) // shorthand for // await cursor.move(selector) // await cursor.click() }

run('https://www.nike.com/de/login');