My Angular website is encountering errors in Google Dev Tools's console when I utilize a Cheerio method
load('<h2 class="title">Hello world</h2>');
as per the instructions on the Github page.
This application is fresh, and the steps I took are as follows:
sudo ng new myProject
,
sudo chmod -R 777 myProject/
,
sudo npm install cheerio --save
,
sudo npm install @types/cheerio --save
.
To preempt any TypeScript compilation errors that may arise, I also executed: sudo npm install stream --save
In my app.component.ts
file, I imported Cheerio using
import * as cheerio from 'cheerio';
and within the component itself:
ngOnInit() {
cheerio.load('<h2 class="title">Hello world</h2>');
}
Upon running ng serve
, the specific error message displayed is:
inherits_browser.js:5 Uncaught TypeError: Cannot read property 'prototype' of undefined
at inherits (inherits_browser.js:5)
at Object../node_modules/cheerio/node_modules/parse5/lib/parser/parser_stream.js (parser_stream.js:27)
at __webpack_require__ (bootstrap:78)
at Object../node_modules/cheerio/node_modules/parse5/lib/index.js (index.js:41)
at __webpack_require__ (bootstrap:78)
at Object../node_modules/cheerio/lib/parse.js (parse.js:5)
at __webpack_require__ (bootstrap:78)
at Object../node_modules/cheerio/lib/cheerio.js (cheerio.js:5)
at __webpack_require__ (bootstrap:78)
at Object../node_modules/cheerio/index.js (index.js:5)
inherits @ inherits_browser.js:5
./node_modules/cheerio/node_modules/parse5/lib/parser/parser_stream.js @ parser_stream.js:27
__webpack_require__ @ bootstrap:78
./node_modules/cheerio/node_modules/parse5/lib/index.js @ index.js:41
__webpack_require__ @ bootstrap:78
./node_modules/cheerio/lib/parse.js @ parse.js:5
__webpack_require__ @ bootstrap:78
./node_modules/cheerio/lib/cheerio.js @ cheerio.js:5
__webpack_require__ @ bootstrap:78
./node_modules/cheerio/index.js @ index.js:5
__webpack_require__ @ bootstrap:78
./src/app/app.component.ts @ main.js:94
__webpack_require__ @ bootstrap:78
./src/app/app.module.ts @ app.component.ts:10
__webpack_require__ @ bootstrap:78
./src/main.ts @ main.ts:1
__webpack_require__ @ bootstrap:78
0 @ main.ts:12
__webpack_require__ @ bootstrap:78
checkDeferredModules @ bootstrap:45
webpackJsonpCallback @ bootstrap:32
(anonymous) @ main.js:1
Furthermore, attempting to fix the issue by placing import { load } from 'cheerio';
at the top of the file has not resolved the error.
The versions of my tools are as follows (all latest or close to it): Angular CLI: 7.3.6 Node: 11.12.0 OS: darwin x64 Angular: 7.2.11
Edit: Following the solution provided in Adding Cheerio.js to an Angular 6 project? did not rectify the issue; instead, it introduced another error in the console:
cheerio.js:5 Uncaught ReferenceError: require is not defined
at cheerio.js:5