facundoolano / google-play-scraper

Node.js scraper to get data from Google Play
MIT License
2.34k stars 632 forks source link

Code: 'ERR_REQUIRE_ESM' #678

Closed walrus543 closed 7 months ago

walrus543 commented 7 months ago

Description:

I'm not a dev but I'm used to using this tool to download original app icons. A similar bug was reported #668 but I don't understand which solution you recommend. Many thanks for your help.

Example code:

My getPic.js file:

let gplay = require('google-play-scraper');
let https = require('https');
let fs = require('fs');
let appIdArray = [

'com.android.chrome', 'com.google.android.googlequicksearchbox'
];

for(let i = 0; i < appIdArray.length ; i++ ){
    gplay.app({appId: appIdArray[i]})
  .then(function(res){
    console.log(res);
    let file = fs.createWriteStream(res.appId + ".png");
    let request = https.get(res.icon, function(response) {
  response.pipe(file);
});
  }).catch(function(err){
    console.error(err);
    });
}

Error message:

❯ node getPic.js
/home/.../getPic.js:1
let gplay = require('google-play-scraper');
            ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/.../node_modules/google-play-scraper/index.js from /home/.../getPic.js not supported.
Instead change the require of index.js in /home.../getPic.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/home/.../getPic.js:1:13) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v21.7.2
walrus543 commented 7 months ago

Solved (thanks to karandpr on XDA developers forum):

getFile.js:

import https from 'https';
import fs from 'fs';
import gplay from "google-play-scraper";
let appIdArray = [

'com.android.chrome', 'com.google.android.googlequicksearchbox'
];

for(let i = 0; i < appIdArray.length ; i++ ){
    gplay.app({appId: appIdArray[i]})
  .then(function(res){
    console.log(res);
    let file = fs.createWriteStream(res.appId + ".png");
    let request = https.get(res.icon, function(response) {
  response.pipe(file);
});
  }).catch(function(err){
    console.error(err);
    });
}

package.json:

{
"type": "module",
  "dependencies": {
    "google-play-scraper": "^10.0.0"
  }
}