jshemas / openGraphScraper

Node.js scraper service for Open Graph Info and More!
MIT License
643 stars 102 forks source link

Enable es modules support #141

Closed MaciejWiatr closed 2 years ago

MaciejWiatr commented 2 years ago

Currently library doesn't seem to work when trying to import it using es module syntax following snippet works just fine:

import { Injectable } from '@nestjs/common';
const ogs = require('open-graph-scraper');
import CreateUrlReq from './dtos/CreateUrlReq.dto';

@Injectable()
export class UrlService {
  private async scrapOgData(url: string) {
    const ogData = await ogs({ url });
    return ogData.result;
  }

  async saveUrl(dto: CreateUrlReq) {
    return await this.scrapOgData(dto.url);
  }
}

but when im trying to import it with following code

import { Injectable } from '@nestjs/common';
import ogs from 'open-graph-scraper';
import CreateUrlReq from './dtos/CreateUrlReq.dto';

@Injectable()
export class UrlService {
  private async scrapOgData(url: string) {
    const ogData = await ogs({ url });
    return ogData.result;
  }

  async saveUrl(dto: CreateUrlReq) {
    return await this.scrapOgData(dto.url);
  }
}

it throws

[Nest] 1852  - 26.11.2021, 20:51:09   ERROR [ExceptionsHandler] (0 , open_graph_scraper_1.default) is not a function
TypeError: (0 , open_graph_scraper_1.default) is not a function
    at UrlService.scrapOgData (C:\Users\macie\Desktop\coding\brainfeed-backend\src\url\url.service.ts:8:29)
    at UrlService.saveUrl (C:\Users\macie\Desktop\coding\brainfeed-backend\src\url\url.service.ts:13:23)
    at UrlController.saveUrl (C:\Users\macie\Desktop\coding\brainfeed-backend\src\url\url.controller.ts:16:28)
    at C:\Users\macie\Desktop\coding\brainfeed-backend\node_modules\@nestjs\core\router\router-execution-context.js:38:29
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at C:\Users\macie\Desktop\coding\brainfeed-backend\node_modules\@nestjs\core\router\router-execution-context.js:46:28
    at C:\Users\macie\Desktop\coding\brainfeed-backend\node_modules\@nestjs\core\router\router-proxy.js:9:17

Can you possibly fix that? 🤔

Thanks for your awesome work!

rachidlamouri commented 2 years ago

Looks like you're using typescript. Setting the esModuleInterop flag to true in the tsconfig compilerOptions will fix the import.

MaciejWiatr commented 2 years ago

It worked, thanks :)