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.

  • Are you trying to install jQuery on Node.js so that you can make a simple HTTP Request? – nicholaswmin Feb 26 '18 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. – Karim Shoorbajee Feb 26 '18 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`. – nicholaswmin Feb 27 '18 at 05: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);` – Aberel Nov 22 '18 at 21:20

1 Answers1

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.
});
Jeremy M.
  • 1,154
  • 1
  • 9
  • 29