1

I have tried npm install jQuery --save and in my node file var $ = require('jquery'); but, then when I run my node file with

$.getJSON('https://en.wikipedia.org/wiki/Washington,_D.C.', function(data) {
    //data is the JSON string
});

I get the error

TypeError: $.getJSON is not a function
    at Object.<anonymous> (C:\Users\Karim\node 2\tweetPic.js:16:3)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3

I have also tried importing jquery using

require("jsdom").env("", function(err, window) {
if (err) {
    console.error(err);
    return;
}

var $ = require("jquery")(window);
});

which just returns

TypeError: require(...).env is not a function
    at Object.<anonymous> (C:\Users\Karim\node 2\tweetPic.js:3:18)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3

I have installed the jsdom package in a similar fashion. Is there something wrong with my jquery code itself? How can I fix this?

Edit: It seems jQuery isn't really what I need here. I'm just going to look into a different way of retrieving json data.

4
  • Are you trying to install jQuery on Node.js so that you can make a simple HTTP Request? Commented Feb 26, 2018 at 17:10
  • Nicholas I'm trying to get the json data from a specific url. I'm not sure if that translates to a simple HTTP request. Commented Feb 26, 2018 at 19:19
  • 1
    It's a simple HTTP request. Save yourself the hassle and follow @JeremyM.'s answer below. jQuery is mostly a front-end framework that simply includes helper functions for sending HTTP requests. It's an overkill to install it on a server just to do something that can be easily done there with other specialised, lightweight modules that are mean to run on a server, like request. Commented Feb 27, 2018 at 5:54
  • Karim, If you are still finding a solution, this is: (updated 2018) var jsdom = require("jsdom"); const { JSDOM } = jsdom; vconst { window } = new JSDOM(); const { document } = (new JSDOM('')).window; global.document = document; var $ = jQuery = require('jquery')(window); Commented Nov 22, 2018 at 21:20

1 Answer 1

2

Sorry but i don't know how to install jquery. But you apparently need it to request a website and fetch json. You could use request for that. Hope i helped you.

And you could do like :

request('https://en.wikipedia.org/wiki/Washington,_D.C.', function (error, response, body) {
  console.log('error:', error); // Print the error if one occurred
  console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
  console.log('body:', body); // Print the HTML for the Google homepage.
});
Sign up to request clarification or add additional context in comments.

4 Comments

Could you explain why console.log(body); would print the html for the google homepage? I'm assuming you meant print the html for the washington dc wikipedia page. Is that correct?
You right mate :)
Maybe you could validate my answer ?
Sorry. Yes this works. Thanks so much!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.