6

I'm using Angular 6 and scraperjs.

and in my component file, used the code given in the example like

import { Component, OnInit } from '@angular/core';

import scraperjs from 'scraperjs';

@Component({
  selector: 'app-express',
  templateUrl: './express.component.html',
  styleUrls: ['./express.component.css']
})
export class ExpressComponent implements OnInit {

  constructor() { }

  ngOnInit() {
    this.aliExpress();
  }

  aliExpress() {
    console.log('loaded ali express');

    scrapperjs.StaticScraper.create('https://news.ycombinator.com')
      .scrape(function ($) {
        return $('.title a').map(function () {
          return $(this).text();
        }).get();
      }).then(function (news) {
      console.log(news);
    });
  }

}

but it is giving error as

Failed to compile.

./node_modules/cheerio/index.js 
Module not found: Error: Can't resolve './package' in '/home/user/code/angular/ali-express/node_modules/cheerio'
4
  • You are trying to load a module using require() at run-time. There has to be a configured module loader that knows where to load files from. The default require() created by WebPack will only map to modules that exist in the bundles. Either make sure they are added to the bundles. Configure a different loader (like SystemJS) or use import. Commented Aug 13, 2018 at 13:37
  • I used import scraperjs from 'scraperjs'; at top and removed require, still same error. Commented Aug 13, 2018 at 13:45
  • Can you try installing cheerio manually. npm install --save-dev cheerio Commented Aug 13, 2018 at 14:04
  • I tried that too. still same issue Commented Aug 14, 2018 at 5:52

3 Answers 3

1

Run

npm install

command to install dependent packages. I think dependent packages are not properly installed.

Sign up to request clarification or add additional context in comments.

1 Comment

all packages are installed. I run it multiple times
1

Remove the nodeModules from your solution folder structure and then try to install npm-install

Comments

0

You are using require() to load the file which is not the standard way of loading a file in Angular. Here, it tries to load at runtime but can't find it.

Please try loading it with import. If you still can't , try loading it in main.ts. I am not sure but I loaded the hammerjs same way.

2 Comments

updated question with import statement. still same error.
Just try import 'scrapperjs' in main.ts

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.