Error: Cannot use import statement outside a module
Hello - I'm trying to use Apify to download a file.
I am following the tutorial here:
https://docs.apify.com/academy/puppeteer-playwright/common-use-cases/downloading-files
But I keep getting this error:
My code:
Is the import statement supposed to go somewhere else? I'm using the code editor in the actor console. I've seen someone else mention putting
"type":"module"
in package.json, but I don't see anywhere to edit package.json in the actor console.
Can you LMK how to import fs, so I can read the downloaded file? Thank you!Downloading files | Apify Documentation
Learn how to automatically download and save files to the disk using two of the most popular web automation libraries, Puppeteer and Playwright.
5 Replies
Hello @Derek Pankaew ,
You are probably using one of these pre-made actor solutions like
puppeteer-scraper
, it is meant to be simplified way how to start with scraping and work with the page context. As mentioned in the description of the actor it is very limited in using external functions and modules.
The documentation you mentioned is related to building you own actor "from scratch". I suggest you to start with https://docs.apify.com/cli/docs and create actor from a puppeteer template.Apify CLI | Apify Documentation
Apify command-line interface (Apify CLI) helps you create, develop, build and run
exotic-emeraldOP•3y ago
Ah, I see - got it. Thank you! I'll learn the CLI and build from there.
unwilling-turquoise•3y ago
@Andrey Bykov in puppeteer-scraper you are limited only on route handler and navigation hooks. This way you may use
import
inside function which would throw compile time error. Using require doesn't work either. I tried https://console.apify.com/view/runs/6GHqKvx3iT8VJ1C96 🙂unwilling-turquoise•3y ago
@Pepa J oh yeah, you're right. I messed it up in my head with multi-file editor 🙃 side note - thanks to my internet at home - I haven't seen your reply 🤯