【问题标题】:Module not found: Error: Can't resolve './package' in Angular 6找不到模块:错误:无法在 Angular 6 中解析“./package”
【发布时间】:2018-08-13 13:26:54
【问题描述】:

我正在使用Angular 6scraperjs

在我的组件文件中,使用了示例中给出的代码

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);
    });
  }

}

但它给出了错误

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'

【问题讨论】:

  • 您正尝试在运行时使用require() 加载模块。必须有一个配置的模块加载器,它知道从哪里加载文件。 WebPack 创建的默认require() 只会映射到捆绑包中存在的模块。要么确保将它们添加到捆绑包中。配置不同的加载器(如 SystemJS)或使用import
  • 我在顶部使用了import scraperjs from 'scraperjs';并删除了require,仍然是同样的错误。
  • 您可以尝试手动安装cheerio。 npm install --save-dev cheerio
  • 我也试过了。还是一样的问题

标签: angular angular6 cheerio


【解决方案1】:

运行

npm install

安装依赖包的命令。 我认为依赖包没有正确安装。

【讨论】:

  • 所有软件包都已安装。我多次运行它
【解决方案2】:

从解决方案文件夹结构中删除 nodeModules,然后尝试安装 npm-安装

【讨论】:

    【解决方案3】:

    您正在使用require() 加载文件,这不是在 Angular 中加载文件的标准方式。 在这里,它尝试在运行时加载但找不到。

    请尝试使用导入加载它。如果仍然不能,请尝试将其加载到main.ts。我不确定,但我以同样的方式加载了hammerjs。

    【讨论】:

    • 使用导入语句更新问题。还是一样的错误。
    • 试试在 main.ts 中导入 'scrapperjs'
    猜你喜欢
    • 2021-11-24
    • 1970-01-01
    • 2019-01-07
    • 1970-01-01
    • 1970-01-01
    • 2017-03-04
    • 1970-01-01
    • 2017-07-06
    • 2018-06-25
    相关资源
    最近更新 更多