How to Import jsdom in a Node Module?

This topic was automatically generated from Slack. You can find the original thread here.

Hi everyone, can I please get some help with importing jsdom in a node module? I am not managing to get it right, if I can just have someone send me what I need type that would be awesome. Thanks!

how can I use jsdom in Pipedream node.js code step?

To use jsdom in a Pipedream Node.js code step, you’ll first need to install the jsdom package. Unfortunately, Pipedream currently does not support installing external npm packages directly. However, you can use an alternative approach by making an HTTP request to fetch the HTML content and then parse it using a built-in package like Cheerio. Here’s an example of how to use Cheerio to parse HTML content in a Pipedream Node.js code step: 1. First, add the following import at the top of your code step: import cheerio from "cheerio"; import { axios } from "@pipedream/platform"; 2. Next, use the axios package to fetch the HTML content from a URL: const response = await axios(this, { method: "GET", url: "https://example.com", }); 3. Then, load the HTML content into Cheerio: const $ = cheerio.load(response); 4. Finally, use Cheerio to parse and extract data from the HTML: const pageTitle = $("title").text(); console.log("Page title:", pageTitle); Here’s the complete example: ```
import cheerio from “cheerio”; import { axios } from “@pipedream/platform”; export default defineComponent({ async run() { const response = await axios(this, { method: “GET”, url: “https://example.com”, }); const $ = cheerio.load(response); const pageTitle = $(“title”).text(); console.log(“Page title:”, pageTitle); }, });

Hi , would you mind checking the response if it is answering your request?

Hi , this may be a bit of a silly question but is $ not already defined (alongside steps) as a parameter of async run()?

the $ variable for cheerio is already defined, I need to rename it

I apologize for the confusion earlier. You’re right, the $ variable is already defined in Pipedream’s context. To avoid conflicts, you can rename the Cheerio variable to something else. Here’s the updated example with the Cheerio variable renamed to cheerioInstance: import cheerio from "cheerio"; import { axios } from "@pipedream/platform"; export default defineComponent({ async run() { const response = await axios(this, { method: "GET", url: "https://example.com", }); const cheerioInstance = cheerio.load(response); const pageTitle = cheerioInstance("title").text(); console.log("Page title:", pageTitle); }, }); Now, the Cheerio variable is named cheerioInstance instead of $, avoiding any conflicts with the existing $ variable in Pipedream’s context.

you can ask Mario any question you’d like

Neat, thanks so much for the help. I’ll ask Mario next time :laughing: