Cannot use import statement outside a module
I am following this guide:
https://developers.apify.com/academy/web-scraping-for-beginners/crawling/pro-scraping#crawlee-installation
I created new folder and run this:
npm install crawlee@latest
Then I create crawlee.js with this:
import { CheerioCrawler } from 'crawlee';
console.log('Crawlee works!');
Then I run this:
node crawlee.js
And I get this:
SyntaxError: Cannot use import statement outside a module
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1033:15)
at Module._compile (node:internal/modules/cjs/loader:1069:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47
Why is that?3 Replies
fascinating-indigoOP•3y ago
Ok I added
"type": "module"
Now it works,
It is not mentioned in this guide,
But it was mentioned earlier in the guide
Thanks
Anyway, I continue the guide and I have another question,
I have this code from here:
https://developers.apify.com/academy/web-scraping-for-beginners/crawling/pro-scraping#coding-the-scraper
import { CheerioCrawler } from 'crawlee';
const crawler = new CheerioCrawler({
requestHandler: async ({ $, request }) => {
console.log('URL: ', request.url);
console.log('Title: ', $('title').text());
},
});
await crawler.addRequests([{ url: 'https://demo-webstore.apify.org/search/on-sale' }]);
And I run: node crawlee.js
But I don't see nothing printed to console,
Why is that?rare-sapphire•3y ago
Try to use the CLI templates,
apify create my-crawler
that will set all files correctly
You are missing await crawler.run()
fascinating-indigoOP•3y ago
Thank you, the code is copied from the Apify link so maybe you want to update it.