Record The Process of using Cheerio Library in Deno Once

deno_cheerio

Record the process of using cheerio library in Deno once.

How to use cheerio in Deno .

cheerio is a very popular npm package, a fast, flexible, and implementable jQuery core implementation specially customized for the server. It can be said that cheerio is a Node.js version of jQuery.

So how do we use this library in Deno?

use

If you use the source code directly in Deno, like this:

import * as Cheerio from "https://raw.githubusercontent.com/cheeriojs/cheerio/v1.0.0/lib/cheerio.js"

Will report an error:

error: Uncaught ReferenceError: require is not defined
var parse = require('./parse'),
            ^
    at https://raw.githubusercontent.com/cheeriojs/cheerio/v1.0.0/lib/cheerio.js:6:13

Because Deno does not support the commonjs specification, only esm is supported.

Therefore, we must use jspm.io (or other similar services) to convert commonjs to a compatible esm format.

We can do this:

import cheerio from "https://dev.jspm.io/npm:cheerio/index.js";

const $ = cheerio.load('<h2 class="title">Hello world</h2>');

$("h2.title").text("Hello Deno!");
$("h2").addClass("deno");

console.log($.html());

Let’s try to run:

deno run mod.ts

The output was successful <h2 class="title deno">Hello Deno!</h2>.

Add TypeScript support

Fortunately, the @types repository provides cheerio type definition files. We add a line at the top of mod.ts:

+// @deno-types="https://dev.jspm.io/@types/cheerio/index.d.ts"
 import cheerio from "https://dev.jspm.io/cheerio/index.js";

Run it, and an error is reported

error: relative import path "node" not prefixed with / or ./ or ../ Imported 
from "https://dev.jspm.io/npm:@types/cheerio@0.22.21/index.d.ts"

It seems that this d.ts file is not compatible with deno. Download this file locally and create a new cheerio.d.ts to transform it.

The problem /// <reference types="node" />lies in line 14, which is not compatible with Deno, so delete this line:

-/// <reference types="node" />
-

Run again, and an error is reported:

error: TS2580 [ERROR]: Cannot find name 'Buffer'. Do you need to install type definitions for node? Try `npm i @types/node`.
  load(html: string | Buffer, options?: CheerioOptionsInterface): CheerioStatic;
                      ~~~~~~
    at https://cdn.jsdelivr.net/gh/justjavac/deno_cheerio/cheerio.d.ts:310:23

Buffer It is the type of nodejs, so an error was reported.

In fact, there are Deno Buffer, we need to use Deno.Bufferto reference, taking into account Deno’s Buffernot compatible and Node.js, so direct delete this type.

-  load(html: string | Buffer, options?: CheerioOptionsInterface): CheerioStatic;
+  load(html: string, options?: CheerioOptionsInterface): CheerioStatic;

Run again, and finally get the result we want:

<h2 class="title deno">Hello Deno!</h2>

example

deno run https://cdn.jsdelivr.net/gh/justjavac/deno_cheerio/mod.ts

Download Details:

Author: justjavac

Source Code: https://github.com/justjavac/deno_cheerio

#deno #nodejs #javascript

Record The Process of using Cheerio Library in Deno Once
5.45 GEEK