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:
Error: Compilation of pageFunction failed.
2023-03-02T03:33:45.814Z Cannot use import statement outside a module
2023-03-02T03:33:45.816Z import fs from 'fs';
2023-03-02T03:33:45.818Z SyntaxError: Cannot use import statement outside a module
Error: Compilation of pageFunction failed.
2023-03-02T03:33:45.814Z Cannot use import statement outside a module
2023-03-02T03:33:45.816Z import fs from 'fs';
2023-03-02T03:33:45.818Z SyntaxError: Cannot use import statement outside a module
My code:
async function pageFunction(context) {

const { page, request, log } = context;

import fs from 'fs';

await page._client.send('Page.setDownloadBehavior', {behavior: 'allow', downloadPath: './my-downloads'})

await page.click('span.dig-Button-content:contains("Download")');

const fileNames = fs.readdirSync('./my-downloads');

// Let's pick the first one
const fileData = fs.readFileSync(`./my-downloads/${fileNames[0]}`);

const base64PDF = Buffer.from(fileData).toString('base64');

return {
url: request.url,
base64PDF: base64PDF
};
}
async function pageFunction(context) {

const { page, request, log } = context;

import fs from 'fs';

await page._client.send('Page.setDownloadBehavior', {behavior: 'allow', downloadPath: './my-downloads'})

await page.click('span.dig-Button-content:contains("Download")');

const fileNames = fs.readdirSync('./my-downloads');

// Let's pick the first one
const fileData = fs.readFileSync(`./my-downloads/${fileNames[0]}`);

const base64PDF = Buffer.from(fileData).toString('base64');

return {
url: request.url,
base64PDF: base64PDF
};
}
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
Pepa J
Pepa J•3y ago
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-emerald
exotic-emeraldOP•3y ago
Ah, I see - got it. Thank you! I'll learn the CLI and build from there.
unwilling-turquoise
unwilling-turquoise•3y ago
Which actor are you using? Looks like it's web scraper -with web scraper - you can't use external packages - you should try puppeteer or playwright.
Pepa J
Pepa J•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
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 🤯

Did you find this page helpful?