-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?

1
  • 1
    You're using Nodejs. Use jquery with require. Like var $ = require('jquery'); Commented Oct 7, 2018 at 11:09

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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 - 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.
I understand but, it's just I'm short on time here and learning how to use http.request will take some time
@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.
Thank you for the link I will definitely check them out in my spare time! I'd like to learn more about Node.js.
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)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.