-1

I'm trying to build a console application using JavaScript/Node.js, the following script.js throws an ReferenceError: $ is not defined upon compilation:

//user input from command line
var readline = require('readline');

var rl = readline.createInterface({
 input: process.stdin,
 output: process.stdout
});

rl.question("Would you like to see which Car Types are available? Please type yes/no ", function(answer) {
// if yes, print search results to console in descending price order 
if (answer === 'yes'){

var results= "";
$.getJSON("https://techtest.rideways.com/dave/?pickup=3.410632,-2.157533&dropoff=3.410632,-2.157533", function(data){
results = JSON.parse(data);
}); 

console.log("The following Car Types are available:", results.options['car_type'], " - ", results.options['price']);
}else{
console.log("No worries, have a nice day!");
}
rl.close();
});

As shown in this post; ReferenceError: $ is not defined I'm hoping its because I'm missing the JS libraries.

Is there a way I can call the JS libraries inside my script.js without creating a separate HTML file? As I'm not building a front end web application therefore don't see the point in creating a HTML file?

Sara
  • 85
  • 1
  • 1
  • 5

2 Answers2

3

In Node.js code, you don't typically need to use jQuery, but if you do want to it's available as an npm module. You'd install it (npm install jquery) and then require it just like you required readline in your code. Full details (since jQuery expects a DOM environment) in this answer.

But, there's no need for jQuery here. Use http.request or https.request or similar instead. I doubt $.getJSON will even work in Node.js.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thank you, I will stick with `require` for now but will look into `http.request`. I'm getting `$.getJSON is not a function` error although I have installed get-json via `npm get-json` – Sara Oct 07 '18 at 11:23
  • @Sara - Really, don't try to use jQuery for network operations on Node.js. Out of curiousity I just gave it a go, and it complained about trying to do a cross-origin request from origin `null`. jQuery just isn't the right tool for this job. – T.J. Crowder Oct 07 '18 at 11:36
  • I understand but, it's just I'm short on time here and learning how to use `http.request` will take some time – Sara Oct 07 '18 at 11:44
  • @Sara - If you're short on time, **definitely** don't try to use the wrong tool for the job. Just look for examples, [like these](https://stackoverflow.com/questions/6287297/reading-content-from-url-with-node-js). – T.J. Crowder Oct 07 '18 at 11:49
  • Thank you for the link I will definitely check them out in my spare time! I'd like to learn more about Node.js. – Sara Oct 07 '18 at 11:57
1

The $ looks like it was a copy/paste from some jQuery code. Instead of jQuery, axios will get the job done in Node and in a browser. axios is an http request library with promises. First, add the package to the project: $ npm i -s axios.

Example get request: (run it on repl.it)

const axios = require('axios');

main();

function main() {
  return exampleRequest().then(renderResponseData)
}

function exampleRequest() {
  const url = 'https://techtest.rideways.com/dave/?pickup=3.410632,-2.157533&dropoff=3.410632,-2.157533';

  return axios.get(url)
}

function renderResponseData(response) {
  console.log(response.data)
}
webprojohn
  • 958
  • 7
  • 14