facundoolano / google-play-scraper

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

Import to TS project fails #659

Open Aliaksandr-Kasko-JazzTeam opened 10 months ago

Aliaksandr-Kasko-JazzTeam commented 10 months ago

Description:

Please sync index.d.ts with index.js. Because of import issue.

Example code:

import gplay from "google-play-scraper";

Error message:

TS1192: Module '"/path/to/project/node_modules/google-play-scraper/index"' has no default export.

If I import with

import * as gplay from "google-play-scraper";

and call, for example, list

gplay.list({country: "us"}).then(console.log, console.warn)

then I got an error

TypeError: gplay.list is not a function

Only one workaround exists for now is to suppress import gplay from "google-play-scraper" with // @ts-ignore

jbigman commented 10 months ago

It works perfectly on my side:

Module fully export methods through index files:

google-play-scraper/index.js

const methods = {
  app: appMethod,
  list,
  search: R.partial(search, [appMethod]),
  suggest,
  developer,
  reviews,
  similar,
  permissions,
  datasafety,
  categories
};
export default Object.assign({ memoized }, constants, methods);

google-play-scraper/index.d.ts, line 320 :

export const list: IFnList

Usage :

import gplay from "google-play-scraper"

server.get('/gplay.list', async (req: any, res: any) => {
    try {
      const result = await gplay.list({ collection: gplay.collection.GROSSING, lang: 'fr', country: 'FR' })
      res.send(result)
    } catch (err: any) {
      res.send('failed ' + JSON.stringify(err?.message))
    }
  })

My tsconfig.json

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "compilerOptions": {
    "target": "ESNext",
    "module": "ESNext",
    "rootDir": "./src/",
    "outDir": "./dist/",
    "strict": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "experimentalDecorators": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "allowSyntheticDefaultImports": true,
    "resolveJsonModule": true,
    "forceConsistentCasingInFileNames": true,
    "removeComments": true,
    "sourceMap": false,
    "baseUrl": "./",
    "typeRoots": [
      "./node_modules/@types",
      "./src/custom_typings"
    ]
  },
  "include": [
    "src/**/*",
    "src/**/*.d.ts",
    "src/**/*.json"
  ],
  "exclude": [
    "node_modules",
    "dist",
    "custom_typings",
    "typings"
  ],
  "ts-node": {
    "esm": true
  }
}
Aliaksandr-Kasko-JazzTeam commented 10 months ago

Oh, thank you @jbigman. I added these changes to my tsconfig:

"module":  "NodeNext" -> "ESNext",
"moduleResolution": "NodeNext" -> "Node"

and all starts working for me! I think there should be some setup manuals.

Offtop: just a small changes to iteration over enums, but these are trifles.