Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: cheerio.fromURL is not a function #3391

Open
user4302 opened this issue Sep 19, 2023 · 6 comments
Open

TypeError: cheerio.fromURL is not a function #3391

user4302 opened this issue Sep 19, 2023 · 6 comments

Comments

@user4302
Copy link

i use the following code

import * as cheerio from 'cheerio';
const data = await cheerio.fromURL(https://www.youtube.com/);

and i get the following error
TypeError: cheerio.fromURL is not a function

@loganknecht
Copy link

I am experiencing this issue as well, except it occurs when I call replaceWith

@loganknecht
Copy link

loganknecht commented Sep 26, 2023

Please disregard. The solution to this was.

const cheerio = require("cheerio");
// ...
const cheerio_file_dom = cheerio.load(current_file_contents);
// ...
const script_elements = cheerio_file_dom("script[src$=.js]");
script_elements.each((index, current_script_elements) => {
    const original_src_value = current_script_elements.attribs["src"];
    const new_value = `{{ require_js(get_asset_url(".${original_src_value}")) }}`;

    cheerio_file_dom(current_script_elements).replaceWith(new_value);
});

I think @user4302 may be incorrectly referencing the wrong object for the .fromURL command as well.

@user4302
Copy link
Author

Please disregard. The solution to this was.

const cheerio = require("cheerio");
// ...
const cheerio_file_dom = cheerio.load(current_file_contents);
// ...
const script_elements = cheerio_file_dom("script[src$=.js]");
script_elements.each((index, current_script_elements) => {
    const original_src_value = current_script_elements.attribs["src"];
    const new_value = `{{ require_js(get_asset_url(".${original_src_value}")) }}`;

    cheerio_file_dom(current_script_elements).replaceWith(new_value);
});

I think @user4302 may be incorrectly referencing the wrong object for the .fromURL command as well.

i was following this guide in the docs
https://cheerio.js.org/docs/api#fromurl
i just saw that i used data instead of $ when assigning the variable
if i used $ would it have worked?

@allison-truhlar
Copy link

allison-truhlar commented Sep 27, 2023

@user4302 I encountered this same issue with .fromURL. I think it is because this function is not yet available in the published npm package for Cheerio. The top of the website mentions "Cheerio's website is still a work-in-progress, and covers Cheerio's next release that isn't available yet." If you look in the Github Cheerio repository, you can find the function .from URL in cheerio/src/batteries. However, in the npm package, this path does not exist.

Using this code achieved the same goal as using fromURL for me:

const response = await axios.get(url);
const html = response.data;

// Use Cheerio to parse the HTML
const $ = cheerio.load(html);

@sonyarianto
Copy link

Hmm I got into this trap as well hehe. The .fromURL() it's not there anymore :)

@TheNoobiCat
Copy link

I agree with @allison-truhlar, performing the get request yourself using something like Axios seems to be the way to go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants